0

我正在编写一个 LINQ 查询来根据用户输入和选择过滤记录。某些输入可能不是来自用户。所以我需要根据给定的输入进行过滤。我尝试只为 5 个可选输入中的 1 个赋值。但是查询没有返回任何内容。请帮我找到正确的查询。看到查询后,您可以更好地理解。

代码

var model = (from items in Db.Items
             where ((items.ItemNo == null || 
                     items.ItemNo == String.Empty) ||
                    ((items.ItemNo.CompareTo(DD.FromItemNo) >= 0) &&
                     (items.ItemNo.CompareTo(DD.ToItemNo) <= 0))) &&
                   (items.InfoTypeId == 0 ||
                    (items.InfoTypeId == DD.InfoType)) &&
                   (items.CreatedOn == null ||
                    (items.CreatedOn >= DD.Start &&
                     items.CreatedOn <= DD.End)) &&
                   (items.StatusId == 0 ||
                    (items.StatusId == DD.Status)) &&
                   (items.LocationId == 0 ||
                    (items.LocationId == DD.Location)) &&
                   (items.CollectionId == 0 ||
                   (items.CollectionId == DD.Collection))
             select new ViewModel()
             {
                 Itemid = items.Id,
                 INo = items.ItemNo,
                 BTags = (from asd in Db.BibContents
                          where asd.BibId == items.BibId &&
                                asd.TagNo == "245" &&
                                asd.Sfld == "a"
                          select asd.Value).FirstOrDefault(),
                 Sid = (from stat in Db.ItemStatus1
                        where stat.Id == items.StatusId 
                        select stat.Description).FirstOrDefault(),

                 Option = DD.Option,
                 CurrItemNo = DD.ItemNumber
             }).ToList();
4

1 回答 1

0

您必须检查DDfor nulls 或0s 的值,而不是s 的值items

var model = (from items in Db.Items
    where 
    (
       (DD.ItemNo == null || DD.ItemNo == String.Empty) 
       || (items.ItemNo.CompareTo(DD.FromItemNo) >= 0 && items.ItemNo.CompareTo(DD.ToItemNo) <= 0)
    ) 
    && (DD.InfoTypeId == 0 || (items.InfoTypeId == DD.InfoType)) 
    && (DD.CreatedOn == null || (items.CreatedOn >= DD.Start && items.CreatedOn <= DD.End)) 
    && (DD.StatusId == 0 || (items.StatusId == DD.Status))
    && (DD.LocationId == 0 || (items.LocationId == DD.Location)) 
    && (DD.CollectionId == 0 || (items.CollectionId == DD.Collection))
    select ...
于 2013-02-21T11:53:28.587 回答