2013-12-23 47 views
4

考慮下面的數據庫結構LINQ MVC視圖模型:多加入到同一個表與可選字段

類別

ID CategoryNameResID ParentCategory(可選)

資源

ID 文本 Lang

並給予一個ViewModel

public class CategoryViewModel 
{ 
    public int ID { get; set; } 
    public int CategoryNameResID { get; set; } 
    public string CategoryName { get; set; } 

    public int ParentCategory { get; set; } 
    public string ParentCategoryName { get; set; } 
} 

我想與ParentCategoryName所有類別的包括

我做了什麼至今列表是:

var categories = (from cat in db.Categories 
        join res in db.Resources on cat.CategoryNameResID equal res.ID 
        select new CategoryViewModel{ 
ID = cat.ID, 
CategoryNameResID = cat.CategoryNameResID, 
CategoryName = res.Text, 
ParentCategory = cat.ParentCategory, 
ParentCategoryName = (from p in db.Resources 
where p.ID == cat.ParentCategory 
select p.Text) 
}).ToList(); 

我無法弄清楚如何獲取ParentCategoryName而不必再次迭代,這肯定是錯誤的。

回答

1

試試這個:

(from cat in cats 
join res in resources on cat.ResId equals res.Id let categoryName = res.Text 
join cat1 in cats on cat.ParentId equals cat1.Id into parentJoin 
from pj in parentJoin.DefaultIfEmpty() let parentCatResId =pj==null?0: pj.ResId 
join res1 in resources on parentCatResId equals res1.Id into resJoin 
from res2 in resJoin.DefaultIfEmpty() let parentName = (res2==null?string.Empty:res2.Text) 
    select new CategoryVM 
    { 
     Id = cat.Id, 
     ResId = cat.ResId, 
     CatName = categoryName, 
     ParentId = cat.ParentId, 
     ParentName = parentName 
    }).ToList(); 
+0

PARENTID不洋關鍵資源,我需要的是ParentID的CategoryResId,因爲ParentID引用了ID和類別 –

+0

希望我更新的答案符合您的要求。 – Arindam

+0

是的,除了這種方法只返回**有** ** ParentCategory的行,我需要的其實是所有的 –

0

假設你有以下數據與表

dbo.Categories 

ID CategoryNameResID ParentCategory 
1 1     NULL 
2 2     NULL 
3 3     1 
4 4     NULL 
5 5     4 
6 6     4 
7 7     4 


dbo.Resources 

ID Text    Lang 
1 Standard   en-GB 
2 Custom    en-GB 
3 Standard Oversize en-GB 
4 Loose    en-GB 
5 Loose 2F Set  en-GB 
6 Loose (4」 Scale) en-GB 
7 Loose (6」 Scale) en-GB 

以下LINQ語句將輸出所需的結果:

public class CategoryViewModel 
{ 
    public int ID { get; set; } 
    public int CategoryNameResID { get; set; } 
    public string CategoryName { get; set; } 

    public int? ParentCategory { get; set; } 
    public string ParentCategoryName { get; set; } 
} 


var categories = (from cat in Categories 
        join res in Resources on cat.CategoryNameResID equals res.ID let categoryName = res.Text 
        select new CategoryViewModel 
        { 
        ID = cat.ID, 
        CategoryNameResID = cat.CategoryNameResID, 
        CategoryName = categoryName, 
        ParentCategory = cat.ParentCategory,  
        ParentCategoryName = Resources.FirstOrDefault(r => r.ID == cat.ParentCategory).Text 
        }).ToList(); 


foreach(var c in categories) 
{ 
    Console.WriteLine(c.CategoryName + " - " + c.ParentCategoryName); 
} 

// Prints 
Standard - 
Custom - 
Standard Oversize - Standard 
Loose - 
Loose 2F Set - Loose 
Loose (4」 Scale) - Loose 
Loose (6」 Scale) - Loose 
+0

'mscorlib.dll中發生類型'System.NotSupportedException'的異常,但未在用戶代碼中處理 附加信息:'Single'和'SingleOrDefault'方法只能用作最終查詢操作。請考慮在此實例中使用方法'FirstOrDefault'。' –

+0

使用FirstOrDefault()的更新答案 – chridam

+0

您沒有從父類別獲取CategoryNameResID,因此無法在Resources表上匹配它... –

相關問題