通过EF6搜索实体,其中字段为空会产生不正确的结果

时间:2017-09-29 00:46:16

标签: linq null entity-framework-6

我正试图通过EF6从我的数据库中提取数据。我想要TicketProcessStatus为null的所有被拒绝或已批准的票证。我希望有11张符合这些条件的票证,但我收到了具有非零TicketProcessStatus值的被拒绝和已批准的票证。我该如何设置where子句?

public IEnumerable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
{            
    IEnumerable<TEntity> results = _dbSet.AsNoTracking().Where(predicate).ToList();
    return results;
}

尝试#1

var closeables = unitOfWorkCraw.Tickets
    .FindBy(w => w.TicketProcessStatus == null &&
            (w.TicketStatus.Equals(TicketStatus.APPROVED) 
            || w.TicketStatus.Equals(TicketStatus.REJECTED)));

尝试#2

string MrNull = null;
var closeables = unitOfWorkCraw.Tickets
    .FindBy(w => object.Equals(w.TicketProcessStatus, MrNull) &&
            (w.TicketStatus.Equals(TicketStatus.APPROVED) 
            || w.TicketStatus.Equals(TicketStatus.REJECTED)));

尝试#3

var closeables = unitOfWorkCraw.Tickets
    .FindBy(w => string.IsNullOrEmpty(w.TicketProcessStatus) &&
            (w.TicketStatus.Equals(TicketStatus.APPROVED) 
            || w.TicketStatus.Equals(TicketStatus.REJECTED)));

回来的一个例子:

  • Ticket#1 :: TicketStatus:Approved,TicketProcessStatus:Success
  • Ticket#2 :: TicketStatus:拒绝,TicketProcessStatus:成功
  • Ticket#3 :: TicketStatus:拒绝,TicketProcessStatus: [NULL]
  • Ticket#4 :: TicketStatus:Approved,TicketProcessStatus: [NULL]

只能退回门票#3和#4。

0 个答案:

没有答案