我正在努力弄清楚如何检索实例的Value
一部分。IGrouping
我有以下内容:
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 ..
}
产生像..这样的元组
如果您想遍历所有项目,这就是这种方式
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();
(在这种情况下,键被视为'int')