2016-02-19 64 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

建议你多加一点解释为什么这是答案,而不仅仅是代码。 –

相关问题