2

在我的数据库中,我有 13 个订单。

下面的代码返回所有这些,如果OrderID = 0and CustomerName = "lorem"

如果我评论该行(OrderID == 0) ?....,它可以正常工作。怎么了 ?

var result = (from x in db.Order
              where
                  (OrderID == 0) ? x.OrderID > 0 : x.OrderID == OrderID
                  &&
                  (string.IsNullOrEmpty(CustomerName)) ? 
                                            !string.IsNullOrEmpty(CustomerName)
                                            :
                                            x.User.Name.Contains(CustomerName)
              select x)
              .ToList();
4

1 回答 1

3

我认为您不能以这种方式在 LINQ 查询中定义条件条件,您可以做的是,例如:

var result = (from x in db.Order where
              ((OrderID == 0 && x.OrderID > 0) ||  
                (OrderID != 0 && x.OrderID == OrderID))
                  &&
                  (string.IsNullOrEmpty(CustomerName)) ? 
                                            !string.IsNullOrEmpty(CustomerName)
                                            :
                                            x.User.Name.Contains(CustomerName)....
于 2012-04-21T07:54:13.410 回答