Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过集合ID、LINQ检索记录_C#_Linq_Collections_Many To Many - Fatal编程技术网

C# 通过集合ID、LINQ检索记录

C# 通过集合ID、LINQ检索记录,c#,linq,collections,many-to-many,C#,Linq,Collections,Many To Many,如果Id存在于我传递的Id集合中,我想检索这些行 这是我到目前为止试过的 var typeOfNeedIds = [1,2,3]; var query = (from up in _context.UserNeeds .Include(u => u.UserNeedTypes).ThenInclude(ut => ut.TypeOfNeed) where (typeOfNeedIds.IsNullOrEmpty() ||

如果Id存在于我传递的Id集合中,我想检索这些行

这是我到目前为止试过的

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();
我也试过了

typeOfNeedIds.Any(id => up.UserNeedTypes.Any(t => t.TypeOfNeedId == id))
但都不管用


我的代码怎么了?非常感谢您的帮助。

这将返回所有
用户需求
,其中
用户需求类型
至少包含一个
类型的需求ID

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();

类似于
collection.Where(item=>typeOfNeedIds.Contains(item.Id))