[英]Retrieving records by collection IDs, LINQ
I want to retrieve the rows if the Id exists in the collection of Ids I'm passing.如果 Id 存在于我传递的 Id 集合中,我想检索行。
Here's what I've tried so far.这是我到目前为止所尝试的。
var typeOfNeedIds = [1,2,3];
var query = (from up in _context.UserNeeds
.Include(u => u.UserNeedTypes).ThenInclude(ut => ut.TypeOfNeed)
where (typeOfNeedIds.IsNullOrEmpty() ||
typeOfNeedIds.All(id => up.UserNeedTypes.Select(t => t.TypeOfNeedId).Contains(id)))
select up).AsNoTracking();
I've also tried我也试过
typeOfNeedIds.Any(id => up.UserNeedTypes.Any(t => t.TypeOfNeedId == id))
But none worked.但没有一个奏效。
What's wrong with my code?我的代码有什么问题? Any help would be highly appreciated.
任何帮助将不胜感激。
This will return all UserNeeds
, where UserNeedTypes
contains at least one of typeOfNeedIds
.这将返回所有
UserNeeds
,其中UserNeedTypes
至少包含一个typeOfNeedIds
。
var typeOfNeedIds = [1,2,3];
var query = _context.UserNeeds.Where(un =>
un.UserNeedTypes.Any(unp =>
typeOfNeedIfs.Contains(unp.TypeOfNeedId)
)
)
.Include(u => u.UserNeedTypes)
.ThenInclude(ut => ut.TypeOfNeed)
.AsNoTracking();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.