【问题标题】:Multiple OR clauses in LINQ statementLINQ 语句中的多个 OR 子句
【发布时间】:2017-09-22 13:26:12
【问题描述】:

我有以下 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();

这个 LINQ 查询应该做什么,它查看 ReservationRequests 表并查找 RequestStatusId = (suppliedRequestStatusId) 和 SubmitterGroupName 应该等于登录用户角色的记录(但在某些情况下,用户未分配给任何角色),它还应该返回用户创建的任何请求。 因此,如果分配给特定组,则基本上返回所有记录,如果有的话,还返回由登录人员创建的请求。

如果用户登录到一个组,但当用户未分配到任何组时,它不会返回正确的结果,则上述查询可以正常工作。如果人员未分配到任何组,则应返回由用户创建的任何记录。

下面是我编写的一个 SQL 查询,它为我的所有案例返回正确数量的记录,我基本上需要我的 LINQ 像这个 sql 但我不确定我在这里缺少什么。

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

有人可以查看我的 LINQ 查询,看看为什么它返回的记录比我的 SQL 不正确吗?谢谢,我已经看了一段时间了!

毕竟这里的所有建议都是我更新的 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();

所以问题:如果loggedInUserRole 为空,此查询是否有效?如果 loggedInUserRole 为 null,那么我只想返回由登录用户创建的记录。

另一个更新:(2017 年 9 月 22 日)上午 9.54 所以我运行了那个声明。在将用户分配到组但未将用户分配到组而不是仅显示登录用户打开的请求的情况下,它可以正常工作,它会返回更多记录。

【问题讨论】:

  • 查询看起来与 sql 查询不同
  • 多余的括号过多会破坏可读性
  • 顺便说一句,你的sql查询也没多大意义,看SubmitterGroupName != null or SubmitterGroupName = null OR...
  • RequestStatusId?.Value == ResStatusId ,试试这个也能给出更清晰的查询
  • (string.IsNullOrEmpty(loggedInUserRole) || s.SubmitterGroupName == loggedInUserRole) 这将是 (string.IsNullOrEmpty(loggedInUserRole) && s.SubmitterGroupName == loggedInUserRole)

标签: c# mysql sql-server linq


【解决方案1】:

incorrect records 是相对的……我猜它返回的记录是正确的。我们只能分析您在此处所做的工作以及差异在哪里:

在您搜索的 linq 查询中

s.SubmitterGroupName == loggedInUserRole

在您搜索的 SQL 语句中

SubmitterGroupName != null or SubmitterGroupName = null 

SubmitterGroupName 不能同时为空和非空。所以我猜,loggedInUserRole 是空的。这是或,所以这总是正确的。然后你只搜索createdby = 'C5188D45-TEST-45BE-8C04-123455733A31'

另外,在 linq-query 中

.Skip(skip).Take(take)

你的 sql 语句中缺少这个

所以我认为你想要的是:

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

【讨论】:

  • @HereToLearn_ 是的,这应该可以。这意味着 SubmitterGroupName 等于 loggedInUserRole 或 CreatedBy 等于 ApplicationSecurityDirector.CurrentUserGuid。应该没问题
  • 所以我运行了该语句。如果用户被分配到一个组,但当用户没有被分配到一个组而不是显示登录用户打开的请求时,它会返回更多记录。
  • 试试这个:s.SubmitterGroupName == loggedInUserRole || (s.SubmitterGroupName == null && s.CreatedBy == ApplicationSecurityDirector.CurrentUserGuid)
【解决方案2】:

我不完全确定您为什么要在其中添加各种检查,但这应该可以:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多