0

我有一个主 VendorProfile 表和一个包含状态代码和日期戳的 1-many VendorHistory 表。下面的查询仅用于检索每个供应商的最新状态(状态代码和日期)。但是,视图允许用户选择任何状态代码的复选框来过滤视图。所以我需要添加一个匹配任何复选框状态选择的 where 子句。

模型图

    public IEnumerable<BrowseStatusModel> BrowseByStatus(int[] StatusSelections)
    {
        IQueryable<BrowseStatusModel> query = _db.VendorProfiles
            .Include("VendorStatusHistory")
            .Include("StatusCodes")
            .Select(s => new BrowseStatusModel
            {
                ProfileID = s.ProfileID,
                Name = s.Name,
                CompanyName = s.CompanyName,
                CompanyDBA = s.CompanyDBA,
                DateCreated = s.DateCreated,
                Status = s.VendorStatusHistories.OrderByDescending(o => o.DateCreated).FirstOrDefault().Id,
                StatusDate = s.VendorStatusHistories.OrderByDescending(o => o.DateCreated).FirstOrDefault().DateCreated
            })
            .OrderBy(x => x.ProfileID);

        foreach (int status in StatusSelections)
        {
            query = query.Where(x => x.Status == status);
        }
        return query;
    }

上面的 foreach 循环有效,但不幸的是创建了 AND 条件,其中所有选择都必须为真,而不是 ANY。我想我必须以某种方式使用带有以下内容的 where 子句,但在正确的语法上没有成功。

.AsQueryable().Any();
4

1 回答 1

2

使用 contains 代替该 foreach 循环

query = query.Where(x => StatusSelections.Contains(x.Status))

于 2013-04-17T19:41:27.117 回答