none
Linq to sql - where clause RRS feed

  • Question

  • hi all,

    below is my sql code

    DECLARE @EMPNAME VARCHAR(50);
    DECLARE @EMPCITY VARCHAR(100);

    SELECT T1.EMPID FROM TABLE1 AS T1 WHERE ( (@EMPNAME IS NULL || T1.EMPNAME = @EMPNAME ) && (@EMPCITY IS NULL || T1.EMPCITY = @EMPCITY) );

    how do i write the same kind of query using LINQ and C#, experts please help me


    Thanks In Advance, Jeyaseelan

    Friday, January 4, 2013 12:26 PM

Answers

  • String  EMPNAME

    String EMPCITY

    Var  record  = (from t in Table1

    WHERE (EMPNAME IS NULL || t. EMPNAME == EMPNAME) && (EMPCITY is null || T. EMPCITY == EMPCITY)

    Select t)

    • Proposed as answer by Rushdy Najath Friday, January 4, 2013 2:16 PM
    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:33 AM
    Friday, January 4, 2013 1:59 PM
  •  Var  record  = from t in Table1 
    
    where (t.EMPNAME == null || t.EMPNAME == EMPNAME) && (t.EMPCITY == null || T.EMPCITY == EMPCITY)
     
    Select t;

    Hope this helps!

    One good question is equivalent to ten best answers.

    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:33 AM
    Friday, January 4, 2013 2:05 PM
  • Hi Jeyaseelan;

    Try your query like this. It should return a collection of EMPID as the original query has.

    string EMPNAME = "The String you are looking for or null";
    string EMPCITY = "Emp City or null";
    
    ObjectContextName db = new ObjectContextName();
    
    var empIds = from T1 in db.TABLE1
                 where (EMPNAME IS NULL || T1.EMPNAME = EMPNAME ) && (EMPCITY IS NULL || T1.EMPCITY = EMPCITY)
                 select T1.EMPID;

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, January 4, 2013 6:17 PM

All replies

  • String  EMPNAME

    String EMPCITY

    Var  record  = (from t in Table1

    WHERE (EMPNAME IS NULL || t. EMPNAME == EMPNAME) && (EMPCITY is null || T. EMPCITY == EMPCITY)

    Select t)

    • Proposed as answer by Rushdy Najath Friday, January 4, 2013 2:16 PM
    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:33 AM
    Friday, January 4, 2013 1:59 PM
  •  Var  record  = from t in Table1 
    
    where (t.EMPNAME == null || t.EMPNAME == EMPNAME) && (t.EMPCITY == null || T.EMPCITY == EMPCITY)
     
    Select t;

    Hope this helps!

    One good question is equivalent to ten best answers.

    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:33 AM
    Friday, January 4, 2013 2:05 PM
  • Hi Jeyaseelan;

    Try your query like this. It should return a collection of EMPID as the original query has.

    string EMPNAME = "The String you are looking for or null";
    string EMPCITY = "Emp City or null";
    
    ObjectContextName db = new ObjectContextName();
    
    var empIds = from T1 in db.TABLE1
                 where (EMPNAME IS NULL || T1.EMPNAME = EMPNAME ) && (EMPCITY IS NULL || T1.EMPCITY = EMPCITY)
                 select T1.EMPID;

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, January 4, 2013 6:17 PM