0

我有两个模型,类别和子类别,一对多的关系。

然后我有一个名为 Notice 的模型,它与 Subcategory(SubcategoryId as FK) 具有一对一的关系。

用户添加他们想要监控的子类别。当该子类别中发生某些事情时,他们会受到注意。

现在我正在尝试打印信息,以便用户了解正在监视哪些子猫的概述,如下所示:

(icon) Category Y
Subcategory - monitored
Subcategory - monitored
Subcategory - not monitored

(icon) Category X
Subcategory - not monitored
Subcategory - monitored
Subcategory - not monitored

目前我已经通过这样做解决了它:

var SubcatsAndCheckedNotices =
                from subcat in db.Subcategories
                join notice in db.Notices.Where(x=>x.CompanyId == company.CompanyId) on subcat.SubcategoryId equals notice.SubcategoryId into prodGroup
                from item in prodGroup.DefaultIfEmpty()
                select new CheckedNoticesViewModel() {CategoryId =subcat.CategoryId, Category = subcat.Category, Subcategory = subcat, Checked = (item.SubcategoryId == null ? false : true) };

几乎可以工作,问题是我需要按照上面的说明将其打印出来,因此需要进行不同的选择(针对类别),这样做我将无法访问其他类别属性,例如图标属性我需要的。

我被卡住了,知道有更好的方法可以做到这一点,但我想不通。

这是我的完整模型:

    public class Category
    {
        public int CategoryId { get; set; }
        public string Icon { get; set; }
        public string Title { get; set; }
        public ICollection<Subcategory> Subcategories { get; set; }
    }
    public class Subcategory
    {
        public int SubcategoryId { get; set; }
        public string Title { get; set; }
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
    }
    public class Notice
    {
        public int NoticeId { get; set; }
        public int SubcategoryId { get; set; }
        public virtual Subcategory Subcategory { get; set; }    
        public int CompanyId { get; set; }
        public virtual Company Company { get; set; }
    }

有什么建议么?

4

2 回答 2

0

我想你正在寻找的是

var Results = db.Notices.Where(x => x.CompanyId == company.CompanyId)
.Select(x => new CheckedNoticesViewModel()
{
CategoryId = x.Subcategory.CategoryId,
Category = x.Subcategory.Category,
Subcategory = x.Subcategory,
Checked = x.SubcategoryId = null ? false : true
}
.GroupBy(y => y.CategoryId);

foreach(var Result in Results)
{
Print(Result.Key.(Information you want to print));
foreach(var CheckedNoticesViewModel in Result)
{
Print(CheckedNoticesViewModel.(Information you want to print);
}
}

编辑:不,也许这个:

foreach(var Category in db.Categories)
{
Print(Category.Title);
foreach (var SubCategoryNotice in Category.Subcategories.GroupJoin(db.Notices, x => x.SubcategoryId, y => y.SubcategoryId, (x, y) => new { SubCategory=  x, Notice =y }))
{
Print(SubCategoryNotice.SubCategory.Title + " " + (SubCategoryNotice.Notice.Any(x => x.CompanyId == 1) ? true: false));
 }

}

于 2013-06-21T16:23:30.947 回答
0

我通过另一种方法解决了这个问题。

一次查询获取选中的子类别,将其与类别一起发送以查看。打印出类别和子类别并 foreach 检查子类别是否存在于 checkedNotices 中。而不是试图加入一切。

                ViewBag.Categories = db.Categories.ToList();


            var checkedNotices =
                from subcategory in db.Subcategories
                join notice in db.Notices.Where(x => x.CompanyId == company.CompanyId) on subcategory.SubcategoryId
                    equals notice.SubcategoryId
                select
                    new CheckedNoticesViewModel()
                        {
                            CategoryId = subcategory.CategoryId,
                            SubcategoryId = subcategory.SubcategoryId,
                        };

            return View(checkedNotices.ToList());
于 2013-06-23T09:34:16.997 回答