2016-02-19 63 views
0

我有一個表「表1」,其結構如下..轉化爲IEnumerable C#

public class Table1 
{ 
    public int Id { get; set; } 
    public string SchemaName { get; set; } 
    public string PropertyName { get; set; } 
    public string PropertyType { get; set; } 
} 

使用實體框架,我可以得到的數據出來作爲一個IEnumerable。

但我需要的數據爲IEnumerable,其結構如下:

public class myschemaobj 
{ 
    public string SchemaName { get; set; } 
    public List<mypropobj> PropertyObjects { get; set; }   
} 

public class mypropobj 
{ 
    public string PropertyName { get; set; } 
    public string PropertyType { get; set; } 
} 

所有幫助表示衷心感謝。

感謝

+2

看起來你需要用'SchemaName'組。你有什麼嘗試? – juharr

回答

0
Table1.GroupBy(r=>r.SchemaName).Select(grp=>new myschemaobj 
{ 
    SchemaName = grp.Key, 
    PropertyObjects = grp.Select(r=>new mypropobj 
    { 
     PropertyName = r.PropertyName, 
     PropertyType = r.PropertyType 
    }) 
    }); 
+1

建議你多加一點解釋爲什麼這是答案,而不僅僅是代碼。 –

相關問題