2012-07-13 56 views
0

如果我有階級結構如下:如何使用動態linq通過嵌套數組進行查詢?

private class A 
    { 
     int afoo { get; set; } 
     B[] bList { get; set; } 
    } 

    private class B 
    { 
     String bfoo { get; set; } 
     C[] cList { get; set; } 
    } 

    private class C 
    { 
     String cfoo { get; set; } 
     int cfoo2 { get; set; } 
    } 

    public class Master 
    { 
     A[] aList; 

     //qry for all where A.B.C.cfoo = "test" 
    } 

我將如何構建一個動態的LINQ語句來查詢爲C類的項目?這樣

1) var qry = aList.blist.clist.AsQueryable().Where("cfoo = \"Test\"").Select(); 

我的最終解決辦法的東西。將通過在這樣的動態部分的完整路徑:

2) var qry = aList.AsQueryable().Where("bList.cList.cFoo = ""Test"").Select(); 

但是,從我都試過,你不能在當嵌套對象。所以我打算使用模板來構建上面1)中的方法。

[此外,我用從斯科特谷的動態庫的動態部分。]

但是,我不能得到那個工作。有什麼建議麼?

謝謝!

回答

2
aList.SelectMany(a => a.bList) 
    .SelectMany(b => b.cList) 
    .Where(c => c.cfoo == "\"Test\""); 
+0

爲了得到這個與動態LINQ工作我需要修改此如下:'aList.SelectMany(A => a.bList) .SelectMany(B = > b.cList).AsQueryable()。Where(「foo = \」Test \「」); ' ' – 2012-07-16 12:48:55

0

嘗試這種情況:

aList.SelectMany(a => a.bList.SelectMany(b => b.cList)) 
    .Where(c => c.cf002 == "Test") 
    .Select();