0

我有兩個模型,分類和子類別,一對多關係。加入問題 - 三張表

然後我有一個名爲Notice的模型,它與子類別(SubcategoryId爲FK)有一對一的關係。

用戶添加他們想監視的子類別。 當子類別內發生某些事情時,他們會注意到它們。

現在我試圖讓用戶得到哪些subcats被監視的概述,這樣打印出來的信息:

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) }; 

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

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

目前我已經做這樣解決了它

這幾乎可行,問題是我需要按照上面的說明打印出來,因此需要做出不同的選擇(對於類別),並且通過這樣做,我失去了訪問其他Category屬性的權限,例如我需要的Icon屬性。

我被卡住了,知道有更好的方法來做到這一點,但我無法弄清楚。

這裏是我的模型在全:

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; } 
    } 

有什麼建議?

+0

你不能只使用類別的導航屬性,然後打印類別,然後打印它的子類型?或者就查詢結果使用GroupBy按類別分組? –

+0

我認爲你錯過了這一點,我需要爲輸出(針對每個子類別)合併用戶特定的通知。 – Josef

+0

那麼你只是想顯示子類別中有「公司」添加的通知的類別和子類別?如果是的話,你可以像我建議的那樣使用GroupBy而不是Distinct? –

回答

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)); 
} 

}

+0

嗯,x和y是不可能的,「不能解析符號'X'」。 – Josef

+0

對不起,添加了x =>新的CheckedNoticesViewModel,雖然你最後的評論表明我的第一個想法實際上並不是你想要的。如果你想打印所有的類別和子類型,並且只有在被監視時纔會被標記,我會選擇2. –

+0

不要認爲選項2會起作用,noticeid和notice不能通過子類別模型訪問。 – Josef

0

我解決它採取另一種方法。

一個查詢獲取檢查的子類別,發送它與類別一起查看。 打印出類別和子類別,然後檢查checkNotices中是否存在子類別。而不是試圖加入一切。

   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());