简体   繁体   English

LINQ语句中的多个OR子句

[英]Multiple OR clauses in LINQ statement

I have below LINQ Query: 我有下面的LINQ查询:

returnVal = context.ReservationRequests
   .Where(s => ((s.RequestStatusId.HasValue) &&
                (s.RequestStatusId.Value == ResStatusId)) &&
               ((string.IsNullOrEmpty(loggedInUserRole) 
                  || s.SubmitterGroupName == loggedInUserRole) 
                  ||(s.CreatedBy == ApplicationSecurityDirector.CurrentUserGuid)))                                                         
   .Skip(skip)
   .Take(take)
  .ToList();

What this LINQ query is supposed to do it look into the ReservationRequests table and look for records where RequestStatusId = (suppliedRequestStatusId) and SubmitterGroupName should be equal to the logged in user role (but there are cases where user is not assigned to any role) and it should also return any requests which are created by user. 该LINQ查询应该执行的操作将调查ReservationRequests表并查找其中RequestStatusId =(suppliedRequestStatusId)和SubmitterGroupName应该等于登录用户角色的记录(但是在某些情况下,用户未分配给任何角色)它也应该返回用户创建的任何请求。 So basically return all records if assigned to a particular group and also the requests created by logged in person if any. 因此,基本上,如果分配给特定组,则返回所有记录;如果有的话,还返回由亲自登录创建的请求。

The above query works fine incases where user is logged in to a group but it does not return correct results when user is not assigned to any groups. 上面的查询在用户登录到组的情况下工作正常,但是当用户未分配给任何组时,它不会返回正确的结果。 In case person is not assigned to any group it should return any records which were CreatedBy the user. 如果未将人员分配到任何组,则应返回该用户创建的所有记录。

Below is a SQL query that I wrote which returns the correct amount of records for all my cases and I basically need my LINQ to be like this sql but I am not sure what I am missing here. 下面是我编写的SQL查询,该查询返回所有案例的正确记录量,我基本上需要LINQ才能像此sql一样,但是我不确定这里缺少什么。

     SELECT *
  FROM [MyDB].[dbo].[ReservationRequests]
  where 
  (RequestStatusId = 2) 
  and 
  (SubmitterGroupName != null or SubmitterGroupName = null 
  or createdby = 'C5188D45-TEST-45BE-8C04-123455733A31')

Can someone please look into my LINQ query and see why is it returning incorrect records than my SQL ? 有人可以调查我的LINQ查询,看看为什么返回的记录比我的SQL错误吗? Thanks I been looking at this for a while now! 谢谢,我已经看了一段时间了!

After all the suggestion here is my updated LINQ: 毕竟这里的建议是我更新的LINQ:

returnVal = context.ReservationRequests
.Where(s => ((s.RequestStatusId.HasValue) &&
(s.RequestStatusId.Value == ResStatusId)) &&
(s.SubmitterGroupName == loggedInUserRole ||
s.CreatedBy == ApplicationSecurityDirector.CurrentUserGuid))
.Skip(skip)
.Take(take)
.ToList();

So question: Will this query work in cases loggedInUserRole is null? 那么问题:在loggingInUserRole为空的情况下,此查询是否有效? In case loggedInUserRole is null then I just want to return records which are CreatedBy the logged in user. 如果LoggedInUserRole为null,那么我只想返回已登录用户CreatedBy的记录。

Another Update: (9/22/2017) 9.54 AM So I ran that statement. 另一个更新:(2017年9月22日)上午9.54,所以我运行了该语句。 It works fine in cases when user is assigned to a group but when user is not assigned to a group instead of showing just the requests opened by the logged in user it returns a lot more records. 在将用户分配给组但未将用户分配给组而不是仅显示已登录用户打开的请求的情况下,它会返回很多记录。

incorrect records is relative... the records it's returning are correct I guess. incorrect records是相对的...我想它返回的记录是正确的。 We only can analyse what you're doing here and where the differences are: 我们只能分析您在这里做什么以及区别在哪里:

In the linq query you search for 在linq查询中搜索

s.SubmitterGroupName == loggedInUserRole

in the SQL statement you search for 在您搜索的SQL语句中

SubmitterGroupName != null or SubmitterGroupName = null 

SubmitterGroupName cant be null and not null at the same time.So I guess, loggedInUserRole is null. SubmitterGroupName不能为null,并且不能同时为null。因此,我猜loggedInUserRole为null。 This is with or, so this is always true. 与or一起使用,因此总是如此。 You only search then for createdby = 'C5188D45-TEST-45BE-8C04-123455733A31' 您只需要搜索createdby = 'C5188D45-TEST-45BE-8C04-123455733A31'

plus, in the linq-query you 另外,在linq查询中

.Skip(skip).Take(take)

this is missing in your sql-statement 这在您的sql语句中丢失

so I think what you want is: 所以我想你想要的是:

returnVal = context.ReservationRequests
.Where(s => ((s.RequestStatusId.HasValue) &&
(s.RequestStatusId.Value == ResStatusId)) &&
(s.SubmitterGroupName == loggedInUserRole || (s.SubmitterGroupName == null && s.CreatedBy == ApplicationSecurityDirector.CurrentUserGuid)))
.Skip(skip)
.Take(take)
.ToList();

I am not entirely sure why you are adding all sorts of checks in there, but this should work: 我不完全确定为什么要在其中添加各种检查,但这应该可行:

returnVal = context.ReservationRequests
.Where(s => RequestStatusId == ResStatusId &&
    (s.SubmitterGroupName == loggedInUserRole || s.CreatedBy == ApplicationSecurityDirector.CurrentUserGuid)
.Skip(skip)
.Take(take)
.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM