我怎么能返回值从一个.net iggrouping通过一个键
本文关键字:一个 iggrouping net 返回值 怎么能 | 更新日期: 2023-09-27 18:10:04
我正在努力弄清楚如何检索IGrouping
实例的Value
部分。
我有以下内容:
IList<IGrouping<string, PurchaseHistory> results = someList.GroupBy(x => 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);
}
}
或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<IGrouping<string, PurchaseHistory> results = someList
.GroupBy(x => x.UserName);
.Select(result => (result.Key, result.Any(SomeStuffExists)));
与. . bool SomeStuffExists(PurchaseHistory item)
{
return ..
}
产生类似…的元组
- ("UserNameX",真的)
- ("UserNameY",假)
- . .
如果你想遍历所有条目
foreach (IGrouping<int, YourClass> value in result)
{
foreach (YourClass obj in value)
{
//Some Code here
}
}
如果你想通过键
来搜索List<YourClass> obj1 = result.Where(a => a.Key
== 12).SingleOrDefault().Where(b=>b.objId.Equals(125)).ToList();
(此处Key被认为是'int')