Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered Linq to sql - where clause

  • Friday, January 04, 2013 12:26 PM
     
     

    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

All Replies

  • Friday, January 04, 2013 1:59 PM
     
     Answered

    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)

  • Friday, January 04, 2013 2:05 PM
     
     Answered Has Code
     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.

  • Friday, January 04, 2013 6:17 PM
     
     Answered Has Code

    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".