C# 如何通过键从.NET I分组返回值?

C# 如何通过键从.NET I分组返回值?,c#,.net,linq,igrouping,C#,.net,Linq,Igrouping,我正在努力找出如何检索I分组实例的值部分 我有以下资料: IList<IGrouping<string, PurchaseHistory> results = someList.GroupBy(x => x.UserName); IList x.UserName); 现在我希望遍历每个集合并检索该用户的购买历史记录(并检查购买历史记录集合中是否存在某些内容)。嵌套循环如何 IList<IGrouping<string, PurchaseHistory>

我正在努力找出如何检索
I分组
实例的
部分

我有以下资料:

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

现在我希望遍历每个集合并检索该用户的购买历史记录(并检查购买历史记录集合中是否存在某些内容)。

嵌套循环如何

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

foreach (IGrouping<string, PurchaseHistory> group in results)
{
    foreach (PurchaseHistory item in group)
    {
        CheckforStuff(item);
    }
}
IList results=someList.GroupBy(x=>x.UserName);
foreach(在结果中对组进行分组)
{
foreach(组中的PurchaseHistory项)
{
检查凝灰岩(项目);
}
}
或一个带有linq语句的循环

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);
}
IList results=someList.GroupBy(x=>x.UserName);
foreach(在结果中对组进行分组)
{
bool result=group.Any(item=>item.PurchasedOn>someDate);
}

如果要遍历所有项目,这是一种方法

foreach (IGrouping<int, YourClass> value in result)
{
    foreach (YourClass obj in value)
    {
      //Some Code here          
    }
}
foreach(结果中的i分组值)
{
foreach(价值上的YourClass对象)
{
//这里有一些代码
}
}
如果你想按键搜索,这就是方法

List<YourClass> obj1 = result.Where(a => a.Key 
== 12).SingleOrDefault().Where(b=>b.objId.Equals(125)).ToList();
List obj1=result.Where(a=>a.Key
==12.SingleOrDefault()。其中(b=>b.objId.Equals(125)).ToList();
(在本例中,键被视为“int”)

调用

IList<IGrouping<string, PurchaseHistory> results = someList
    .GroupBy(x => x.UserName);
    .Select(result => (result.Key, result.Any(SomeStuffExists)));
产生类似..的元组

  • (“UserNameX”,true)
  • (“用户名”,false)

使用
foreach
而不是执行第二次循环如何-是否有方法检查是否存在具有。。。我不知道
item.PurchasedOn>someDate
?嗯-我试过了。。。啊!!我试过结果。任何(…)。。哎呀!它在
组中。任何(…)
。助教。知道了。
bool SomeStuffExists(PurchaseHistory item)
{
    return ..
}