2

我有一个包含三列的表,我想使用自联接并检索具有别名的列。

桌子:Material(ID,Name,MaterialParentID)

public List<Material> GetMaterialList()
{
    List<Material> materilaList = new List<Material>();
    var query = (from c1 in db.Materials
                 join c2 in db.Materials on c1.ID equals c2.MaterialParentID
                 select c2);        

    return query.ToList();
}

我想在现有查询中添加以下内容并返回列表

select new { c2.ID, c2.MaterialParentID, c2.Name, ParentName = c1.Name })
4

2 回答 2

6

只需使用实际的具体类型而不是匿名类型:

select new Material { 
    ID = c2.ID, 
    MaterialParentID = c2.MaterialParentID, 
    Name = c2.Name,
    ParentName = c1.Name 
}
于 2013-07-17T07:58:29.520 回答
-1
foreach(var row in query)
{
    material mat =new material();
    mat.id=row.ID;
    mat.name=row.Name;

    materilaList.add(mat);
}

return materiList;
于 2015-02-27T12:13:56.810 回答