My query below, what im trying to do is get all the records with that quoteID and then group by that ID
var CurSuppliers = db.Quotes_Items_Suppliers.Where(i => i.QuoteID == QuoteID).Select(t => t.SupplierID).GroupBy(t => t).ToList();
I knew i had to only select the 1 field as LINQ would not know how to group if there were more fields with different values.
so im just trying to get some grouped IDs i can loop through, at the moment when i do a foreach on the below i get
strCurSupps = "System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[System.Int32,System.Int32],System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[System.Int32,System.Int32]"
Yes, you're getting a list of groups. When you iterate over that, each element of the iteration will be a group - which you can also iterate over. For example:
foreach (var group in currentSuppliers)
{
Console.WriteLine("Group key: {0}", group.Key);
foreach (var element in group)
{
Console.WriteLine(" Element: {0}", element);
}
}
However, it's not clear what you're hoping to achieve to be honest - you're just going to get a sequence of groups, each of which will consist of a bunch of identical elements, because you're just getting the supplier ID. If you want the whole supplier, you need to get rid of the Select(t => t.SupplierID)
and instead just use GroupBy(t => t.SupplierID)
. Even then, you'll have a sequence of groups, each of which will contain several objects with the same SupplierID
and the same QuoteID
, presumably representing the same entity.
See more on this question at Stackoverflow