LINQ中有3个where子句

本文关键字:子句 where 3个 中有 LINQ | 更新日期: 2023-09-27 18:20:52

我在LINQ中有3个where子句,但它失败了-我尝试了这个

List<string> companies = new List<string>() { "0001001429"};
List<string> roleIDs = new List<string>() { "1486334", "1419282"};

var q = (from up in UserReports
        where up.UserType == "Internal"     
        where companies.Contains(up.CompanyId) && roleIDs.Contains(up.RoleId)
                             select new 
                             {
                                 UserId = up.UserId,
                                 FirstName = up.FirstName,
                                 LastName = up.LastName, ...});

我还尝试了2次加入-

var q = (from up in UserReports
                             join c in companies on up.CompanyID equals c
                             join r in rolesIDs on up.RoleId equals r
                             where up.UserType == "Internal"
                             select new 
                             {
                                 UserId = up.UserId,..});

我在这里做错了什么?

谨致问候,Bhavik

LINQ中有3个where子句

试试这个。我把"where"改成了"and"

var q = (from up in UserReports
    where up.UserType == "Internal"     
    && companies.Contains(up.CompanyId) && roleIDs.Contains(up.RoleId)
                         select new 
                         {
                             UserId = up.UserId,
                             FirstName = up.FirstName,
                             LastName = up.LastName, ...});

您需要指定公司和角色的id:

var q = (from up in UserReports
                         join c in companies on up.CompanyID equals c.Id
                         join r in rolesIDs on up.RoleId equals r.Id
                         where up.UserType == "Internal"
                         select new 
                         {
                             UserId = up.UserId,..});