0

我对包含在 linq to sql 查询中的问题如下:

 public IAuditRecord[] Fetch(SearchConditions searchConditions)
    {
        IAuditRecord[] searchedList = (from rows in _dbContex.AuditTrails
                                         where 
                                         (searchConditions.Owner == null || searchConditions.Owner == 0) ? true : rows.Owner == searchConditions.Owner
                                         &&
                                         /*This line cannot compile when ActionIDs array is empty*/
                                         (searchConditions.ActionIDs != null && searchConditions.ActionIDs.Length != 0) ? searchConditions.ActionIDs.Contains(rows.UserActionID) : true
                                         && ((searchConditions.StartDate != null && searchConditions.EndDate != null) ?  (rows.TimeStamp >= searchConditions.StartDate && rows.TimeStamp <= searchConditions.EndDate)
                                         : (searchConditions.StartDate != null && searchConditions.EndDate == null) ?  rows.TimeStamp >= searchConditions.StartDate : (searchConditions.StartDate == null && searchConditions.EndDate != null) ?  (rows.TimeStamp <= searchConditions.EndDate)
                                         : true)
                                       select rows).ToArray();
        return searchedList;
    }

如果 searchCondition.ActionIDs 数组不为 null 或为空,则此查询将完美执行,但是当我将 ActionIDs 数组作为 null 传递时,无法编译查询。

所以主要问题是为什么当 ActionIDs 数组为空时 contains 不能工作?

4

1 回答 1

0

您正在构建一个IQueryable,它定义了如何查询某些内容,而不是实际执行它。为此,它构建了一个Expression,它定义了所有查询意图,以后可以调用以实际获取数据。如果您使用的是 LINQ-to-Objects,这可能会起作用,因为它可能会searchConditions.ActionIDs != null首先调用,然后知道它不必尝试执行第二部分。Linq-to-Entities/SQL 等没有这个好处。

长话短说,你可以这样做:

searchConditions.ActionIDs = searchConditions.ActionIDs ?? new int[];

或者如果它为空,则执行不同的查询,例如:

var query = _dbContext.AuditTrails;

if(searchConditions.ActionIDs != null && searchCondition.ActionIDs.Length != 0)
{
    query = // Further filtered query where ActionIDs are taken into account.
于 2013-08-01T19:47:20.447 回答