locked
adding conditions to the where clause conditionally for incremental filtering the record RRS feed

  • Question

  • User-1355965324 posted

    I am trying to give where clause with existing linq conditionally  inside the if condition , But not working please help.

    Here is my linq. if cusotmer id and product id   should be  given the where clause only if there is value is being passed .

     public IEnumerable<SalesModel> GetSalesList(DateTime DateFrom, DateTime DateTo, int customerId,int productId)
            {
                IEnumerable<SalesModel> saleslist = 
               (from sales in Context.DailySales
                   join customer in Context.Customer on sales.CustomerID   equals  customer.CustomerID
                    where  sales.InvoiceDate >= DateFrom   && sales.InvoiceDate  <= DateTo
                    if(customerId >0 , sales.CustomerId == customerId )
                    If(productId > 0 , sales.ProductID ==  productId)
            select new SalesModel
           {
              Id = sales.id
              ProductID = sales.ProductID,                                                           
              Qty = sales.Qty,
              Rate = sales.Rate,
              InvoiceDate= sales.InvoiceDate,
              Customer = sales.Customer                                                               
    
                });
                return saleslist;
            }

    Tuesday, September 22, 2020 6:42 PM

Answers

  • User475983607 posted

    The condition pattern is simply...

    (customerId == 0 || sales.CustomerId == customerId)

    ... but your code does not follow openly published standards.

    See the EF Core documentation to learn the syntax.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2020 7:47 PM