开发者

Linq GroupBy how to return a collection of objects that are grouped into another list?

开发者 https://www.devze.com 2023-03-23 13:33 出处:网络
I\'m having a difficult time trying to return a collection of objects after I use Linq to do a GroupBy on the collection.

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.


ToList()

return segmentGroups.ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消