1

I want to write if condition inside where clause. because if first name or last name is null I don't wani to add it in where clause . other wise I want to add it.

so I write

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();

What is the wrong with my cording??

1 Answer 1

2

Note that your original query selects an anonymous type with two properties: opvTable and grTable. So, you need to use one of those properties in the subsequent Where clause, like this:

   query1 = query1.Where(item => item.grTable.first_name == Fname);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.