我有一个包含三列的表,我想使用自联接并检索具有别名的列。
表: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();
}
我想在现有查询中添加以下内容并返回List
select new { c2.ID, c2.MaterialParentID, c2.Name, ParentName = c1.Name })
答案 0 :(得分:6)
只使用实际的具体类型而不是匿名类型:
select new Material {
ID = c2.ID,
MaterialParentID = c2.MaterialParentID,
Name = c2.Name,
ParentName = c1.Name
}
答案 1 :(得分:-1)
foreach(var row in query)
{
material mat =new material();
mat.id=row.ID;
mat.name=row.Name;
materilaList.add(mat);
}
return materiList;