1

我想在 where 子句中写 if 条件。因为如果名字或姓氏为空,我不想将它添加到 where 子句中。否则我想添加它。

所以我写

var query1 = from opv in _opvRepository.Table
                         join o in _orderRepository.Table on opv.OrderId equals o.Id
                         join gr in _graduandRepository.Table on opv.graduand_id equals gr.graduand_id
                         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
                         join p in _productRepository.Table on pv.ProductId equals p.Id
                         where (opv.ceremony_id == ceremony_id) && 
                         (!o.Deleted) && (opv.IsHireItem == true)  &&
                         (!p.Deleted) &&
                         (!pv.Deleted) && (opv.ceremony_id == ceremony_id)      
                       //  group opv by opv.OrderId into g
                         select new
                         {
                             opvTable = opv,
                             grTable = gr,

                         };


// This is not working. I have problem in here. How to add this??
             if (!String.IsNullOrEmpty(Fname))
                 query1 = query1.Where(grTable = > grTable.first_name == Fname);




            var result = query1.ToList().Select(x =>
            {
                return new HireItemReportLine()
                {
                    OrderId = x.opvTable.OrderId,
                    OrderDate=x.opvTable.Order.CreatedOnUtc,
                    Amount= x.opvTable.Order.OrderSubtotalExclTax,
                    PaymentMethod = x.opvTable.Order.PaymentMethodSystemName,
                    paidDate = x.opvTable.Order.CreatedOnUtc,

                    Fname = x.grTable.first_name,
                    MName = x.grTable.middle_name,
                    LName = x.grTable.last_name,



                };
            }).ToList();

我的线有什么问题??

4

1 回答 1

2

请注意,您的原始查询选择具有两个属性的匿名类型:opvTablegrTable。因此,您需要在后续Where子句中使用这些属性之一,如下所示:

   query1 = query1.Where(item => item.grTable.first_name == Fname);
于 2012-11-08T00:26:38.073 回答