[英]linq - find item in list within multiple lists
I have a highly nested class, and trying to find a single item buried deep within. 我有一个高度嵌套的课程,试图找到一个埋在其中的物品。 The following gives me an error "Can't convert type match to bool', although I don't see why it thinks I'm trying to return a boolean. 以下内容给我一个错误“无法将类型匹配转换为布尔值”,尽管我不明白为什么它认为我正在尝试返回布尔值。
var match = community.TeamLeagues
.Where(x => x.Seasons
.Where(y => y.Divisions
.Where(z => z.Matches
.Where(a => a.Id == "1234").FirstOrDefault())));
Where
by itself returns a (deferred) enumerable of items and cannot as such be used as a condition by the outer Where
. Where
本身返回(递延)可枚举的项目,因此不能由外部Where
用作条件。 What you probably want to do is to use Contains()
, Any()
or All()
inside the outer Where
s that will return the result you're looking for. 您可能想要做的是在外部Where
内使用Contains()
, Any()
或All()
,这将返回您要查找的结果。
Something like this might be what you're after: 这样的事情可能就是您想要的:
var match = community.TeamLeagues.Where(t =>
t.Seasons.Any(
s => s.Divisions.Any(
d => d.Matches.Any(
m => m.Id == "1234")
)));
The Where
method needs to evaluate an expression that returns a bool
. Where
方法需要评估返回bool
的表达式。 Your nested Where
s are not doing that - the only Where that is, is the last one a => a.Id == "1234"
, all the other expressions are returning an IEnumerable
. 您的嵌套Where
不这样做-唯一的Where是最后一个a => a.Id == "1234"
,所有其他表达式都返回IEnumerable
。
z.Matches.Where(a => a.Id == "1234").FirstOrDefault()
returns a object of type Match
(your collection item type of the IEnumerable Matches
) (or null), no boolean value. z.Matches.Where(a => a.Id == "1234").FirstOrDefault()
返回Match
类型的对象(您的IEnumerable Matches
集合项类型)(或为null),没有布尔值。 I guess you need to check if there are entires in matches that have a Id 1234. Use Any
to evaluate a condition: 我猜您需要检查匹配项中是否有ID为1234的整数。请使用Any
评估条件:
var match = community.TeamLeagues.Where(x =>
x.Seasons.Any(y =>
y.Divisions.Any(z =>
z.Matches.Any(a => a.Id == "1234")
)));
[ items.Where(x => x.Id == 4).Any()
is the same as items.Any(x => x.Id == 4)
] [ items.Where(x => x.Id == 4).Any()
与items.Any(x => x.Id == 4)
]
This returns you all TeamLeagues which contain a Season which contain a Division which contain a Match which has a element with the id 1234. 这将返回所有包含Season的TeamLeague,其中的Season的Division的Match的ID为1234。
To make it simple you can also use the Matches
table directly and using a ViewModel you can represent your view. 为了简单起见,您还可以直接使用Matches
表,并使用ViewModel可以表示您的视图。
like: var MyViewModel = (from l in Mathes where l.Id == "1234" select new MyViewModel { Id = l.Id, MatchName = l.Name, }).ToList(); 例如:var MyViewModel =(从Math中的l,其中l.Id ==“ 1234”选择新的MyViewModel {ID = l.Id,MatchName = l.Name,})。ToList();
Couldn't get it working with linq, but works with query syntax. 无法使其与linq一起使用,但可以与查询语法一起使用。
var leagueMatch = (from teamLeague in community.TeamLeagues
from season in teamLeague.Seasons
from division in season.Divisions
from match in division.Matches.Where(x => x.Id == "1234")
select match).FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.