是否可以将 LINQ GROUP BY 的结果放入 IEnumerable 列表中
Is it possible to get the result from LINQ GROUP BY into the IEnumerable List
我有以下两个模型
public class DepotDeptMapModel
{
public string id { get; set; }
public string title { get; set; }
public List<DeptModel> subs { get; set; }
}
public class DeptModel
{
public string id { get; set; }
public string title { get; set; }
}
我正在使用给定的 LINQ 查询来获取结果
var list = goContext.goDepartmentWorkTime.
GroupBy(d => new { d.DepotID, d.Depot.DepotName })
.Select(g => new
{
id = g.Key.DepotID,
title = g.Key.DepotName,
subs = g.Select(dd => new
{
id = dd.DepotID + "." + dd.DepartmentID,
title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
})
});
是否可以将查询结果放入列表模型中
List<DepotDeptMapModel> deptList
如有任何帮助,我们将不胜感激
您需要做的就是在 new
调用中使用您的实际类型,而不是匿名类型。但是,由于您想在客户端而不是数据库语句的一部分执行此操作,因此您必须告诉您的上下文在特定点将控制权切换到客户端,这就是 .AsEnumerable()
所做的。
var list = goContext.goDepartmentWorkTime
.GroupBy(d => new { d.DepotID, d.Depot.DepotName })
.AsEnumerable()
.Select(g => new DepotDeptMapModel
{
id = g.Key.DepotID,
title = g.Key.DepotName,
subs = g.Select(dd => new DeptModel
{
id = dd.DepotID + "." + dd.DepartmentID,
title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
})
});
Is it possible to get the query result into the list model List<DepotDeptMapModel> deptList
?
当然可以,如果您的 类 是简单的 POCO(= 只有 get/set 属性),这就很容易了。您所要做的就是更改 Select:
.Select(g => new {...
进入:
.Select(g => new DepotDeptMapModel {...
在完整查询下方,稍作优化:Select 在 GroupBy 中使用参数 resultSelect 或:
完成
List<DepotDeptMapModel> deptList = goContext.goDepartmentWorkTime.GroupBy(
// parameter keySelector: make groups of departments with same key:
department => new DepotDeptMapModel
{
department.DepotID,
department.Depot.DepotName,
})
// parameter ResultSelector, take each key and all departments with this key
// to make one new DepotDeptMapModel
(key, departmentsWithThisKey) => new
{
id = key.DepotID,
title = key.DepotName,
subs = departmentsWithThisKey.Select(department => new DeptModel
{
id = department.DepotID + "." + department.DepartmentID,
title = department.Depot.DepotNo + "." + department.Department.DepartmentName
})
.ToList(),
})
.ToList();
我有以下两个模型
public class DepotDeptMapModel
{
public string id { get; set; }
public string title { get; set; }
public List<DeptModel> subs { get; set; }
}
public class DeptModel
{
public string id { get; set; }
public string title { get; set; }
}
我正在使用给定的 LINQ 查询来获取结果
var list = goContext.goDepartmentWorkTime.
GroupBy(d => new { d.DepotID, d.Depot.DepotName })
.Select(g => new
{
id = g.Key.DepotID,
title = g.Key.DepotName,
subs = g.Select(dd => new
{
id = dd.DepotID + "." + dd.DepartmentID,
title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
})
});
是否可以将查询结果放入列表模型中
List<DepotDeptMapModel> deptList
如有任何帮助,我们将不胜感激
您需要做的就是在 new
调用中使用您的实际类型,而不是匿名类型。但是,由于您想在客户端而不是数据库语句的一部分执行此操作,因此您必须告诉您的上下文在特定点将控制权切换到客户端,这就是 .AsEnumerable()
所做的。
var list = goContext.goDepartmentWorkTime
.GroupBy(d => new { d.DepotID, d.Depot.DepotName })
.AsEnumerable()
.Select(g => new DepotDeptMapModel
{
id = g.Key.DepotID,
title = g.Key.DepotName,
subs = g.Select(dd => new DeptModel
{
id = dd.DepotID + "." + dd.DepartmentID,
title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
})
});
Is it possible to get the query result into the list model
List<DepotDeptMapModel> deptList
?
当然可以,如果您的 类 是简单的 POCO(= 只有 get/set 属性),这就很容易了。您所要做的就是更改 Select:
.Select(g => new {...
进入:
.Select(g => new DepotDeptMapModel {...
在完整查询下方,稍作优化:Select 在 GroupBy 中使用参数 resultSelect 或:
完成List<DepotDeptMapModel> deptList = goContext.goDepartmentWorkTime.GroupBy(
// parameter keySelector: make groups of departments with same key:
department => new DepotDeptMapModel
{
department.DepotID,
department.Depot.DepotName,
})
// parameter ResultSelector, take each key and all departments with this key
// to make one new DepotDeptMapModel
(key, departmentsWithThisKey) => new
{
id = key.DepotID,
title = key.DepotName,
subs = departmentsWithThisKey.Select(department => new DeptModel
{
id = department.DepotID + "." + department.DepartmentID,
title = department.Depot.DepotNo + "." + department.Department.DepartmentName
})
.ToList(),
})
.ToList();