none
linq2sql where clause with nullable filter values RRS feed

  • Question

  • I have 2 textboxes: tbProductCode , tbProductName

    in linq2sql I want

    1. if tbProductCode.text == null then ignore it in where clause, i.e. don't filter by productcode

    2. if tbProductName.text == null then ignore it in where clause, i.e. don't filter by productName

    is it possible to achieve this? thanks in advance


    Achievement provides the ultimate pleasure in life
    Friday, October 15, 2010 3:41 AM

Answers

  • Hi,

    I will try with something like this:

    var query = ... // Initialize here your query
    
    if (!string.IsNullOrEmpty(tbProductCode.text))
       query += query.Where(p => p.Code == tbProductCode.text);
    
    if (!string.IsNullOrEmpty(tbProductName.text))
       query += query.Where(p => p.Name == tbProductName.text);
    

    Hope this can help you,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by liurong luo Monday, October 18, 2010 9:08 AM
    Friday, October 15, 2010 7:14 AM