You must Sign In to post a response.
  • Category: LINQ

    How to write Linq query for below requirement

    Hiiii All
    i got a requirement that is ,i have two fields one is dropdown and textbox ,
    it should display the records based on the entered text in the textbox and dropdown value
    if the textbox value null ,dropdown value null then it should display all the records

    please suggest me how to write Linq query for the above requirement
  • #758873
    Hello Nagendra,

    Refer the below example:
    Query for Records based on textbox and dropdown value:
    var queryCustomers = from cust in customers
    where cust.City == '" + DropDownList1.SelectedValue + "' && cust.Name == '" + TextBox1.Text + "'
    select cust;

    Query for all records
    var queryCustomers = from cust in customers
    select cust;

    Hope this will help you.
    Mark the answer if it helped you.

    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #758881
    i need single Linq Query for the above recuriment

  • #760683
    You can refer following code

    var Query = lstCustomer.ToList();
    if (!string.IsNullOrEmpty(drpCustomer.SelectedText))
    Query = Query.Where(x => x.CustomerName ==drpCustomer.SelectedText).ToList();
    if (!string.IsNullOrEmpty(TxtAddress))
    Query = Query.Where(x => x.Address == TxtAddress).ToList();
    List<Customer> lstResult = Query.ToList<Customer>()

    Umesh Bhosale

  • Sign In to post your comments