score:2

Accepted answer

Assuming the OwnerNames will be identical for all the items in the group then you could do something like:

var linqQuery2 = (from f in linqQuery.ToList()
    group f by f.OwnerId into myGroup
    select new
    {
        OwnerName = myGroup.First().OwnerName,
        OrderCount = myGroup.Count()
    });

Edit:

If you want to use the first item multiple times, I'd suggest you use the 'let' operator:

var linqQuery2 = (from f in linqQuery.ToList()
                    group f by f.OwnerId into myGroup
                    let first = myGroup.First()
                    select new
                    {
                        OwnerName = first.OwnerName,
                        OrderCount = myGroup.Count()
                    });

score:1

Try

        var linqQuery2 = (from f in linqQuery.ToList()
                          group f by f.OwnerName
                          into myGroup
                          select new
                                     {
                                         OwnerName = myGroup.Key,
                                         OwnerCount = myGroup.Count()
                                     });

Alternately:

        var linqQuery2 = linqQuery.ToList()
            .GroupBy(f => f.OwnerName)
            .Select(myGroup => new
                                {
                                    OwnerName = myGroup.Key,
                                    OwnerCount = myGroup.Count()
                                });

Related Articles