Linq GroupBy 如何返回分组到另一个列表中的对象集合?
在使用 Linq 对集合执行 GroupBy 操作后,我在尝试返回对象集合时遇到了困难。
具体来说,当我从 EF 4.1 调用视图时,会返回 CurrentSegmentGroupDetail 对象的集合。使用 Lambda 表达式,我按 SegmentGroup 属性对 CurrentSegmentGroupDetail 对象进行分组。我得到的结果是包含 CurrentSegmentGroupDetail 对象的 SegmetGroup 列表。我遇到的问题是尝试将分组结果集返回到列表类型。
这是我到目前为止的代码:
public List<CurrentSegmentGroupDetail> GetSegmentGroupsForReconciliation()
{
using (var context = new PricingContext())
{
var segmentGroups =
context.CurrentSegmentGroupDetails.GroupBy(s => s.SegmentGroup).Select(y => y);
return segmentGroups;
}
}
这是当我尝试将结果集传递到我的 List 对象时遇到的异常:
“无法隐式转换类型 'System.Linq.IQueryable>'到“System.Collections.Generic.List”,
我将非常感谢对此的任何帮助。
I'm having a difficult time trying to return a collection of objects after I use Linq to do a GroupBy on the collection.
The specifics are, I have a collection of CurrentSegmentGroupDetail objects being returned when I call a view from EF 4.1. Using a Lambda expression, I group the CurrentSegmentGroupDetail object by a SegmentGroup property. The result I get is a list of SegmetGroups that contain CurrentSegmentGroupDetail objects. The problem I'm having is trying to return the grouped result set back to a type of List.
Here is the code I have so far:
public List<CurrentSegmentGroupDetail> GetSegmentGroupsForReconciliation()
{
using (var context = new PricingContext())
{
var segmentGroups =
context.CurrentSegmentGroupDetails.GroupBy(s => s.SegmentGroup).Select(y => y);
return segmentGroups;
}
}
Here is the exception I'm getting when I try and pass the result set into my List object:
"Cannot implicitly convert type 'System.Linq.IQueryable>' to 'System.Collections.Generic.List'.
I would greatly appreciate any help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列表()
ToList()