Results 1 to 2 of 2

Thread: LINQ "where" clause

  1. #1
    Join Date
    Sep 2006

    LINQ "where" clause

    Hi guys,

    Does any one know how to put conditional statements into the LINQ "where" clause. The reason is I have a search form to which all the fields are optional and I need to tell LINQ how to search depending on if the fields are filled out or not.

    Thanks in advance!

  2. #2
    Join Date
    Feb 2009

    re: Linq "where"

    var query = from p in TestData.PayItem().AsEnumerable()
    join b in TestData.PriceBookItem().AsEnumerable() on p.Field<string>("ItemCode") equals b.Field<string>("ItemCode")
    into payItems
    from b in payItems.DefaultIfEmpty()
    select new {
    ItemCode = p.Field<string>("ItemCode"),
    Title = p.Field<string>("Title"),
    Extendable = p.Field<string>("Extendable"),
    IsLumpSum = p.Field<string>("IsLumpSum"),
    UnitCost = b == null ? "" : b.Field<string>("UnitCost")

    if (txtPriceBookTitle.Text.Length > 0)
    query = query.Where(p => p.Title.ToLower().StartsWith(txtPriceBookTitle.Text.ToLower()));

    if (txtPriceBookItemCode.Text.Length > 0)
    query = query.Where(p => p.ItemCode.ToLower().StartsWith(txtPriceBookItemCode.Text.ToLower()));

    dgPriceBook.DataSource = query.ToDataTable();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center