Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return the Value from a .NET IGrouping via a Key?

I'm struggling to figure out how to retrieve the Value part of an IGrouping instance.

I have the following:

IList<IGrouping<string, PurchaseHistory> results = someList.GroupBy(x => x.UserName);

And I now wish to iterate through each collection and retrieve the purchase histories for that user (and check if some stuff exists in the collection of purchase histories).

like image 791
Pure.Krome Avatar asked Sep 27 '22 22:09

Pure.Krome


1 Answers

how about a nested loop?

IList<IGrouping<string, PurchaseHistory>> results = someList.GroupBy(x => x.UserName);

foreach (IGrouping<string, PurchaseHistory> group in results)
{
    foreach (PurchaseHistory item in group)
    {
        CheckforStuff(item);
    }
}

or one loop with linq statement

IList<IGrouping<string, PurchaseHistory>> results = someList.GroupBy(x => x.UserName);
foreach (IGrouping<string, PurchaseHistory> group in results)
{
    bool result = group.Any(item => item.PurchasedOn > someDate);
}
like image 119
fubo Avatar answered Oct 07 '22 09:10

fubo