none
How to write an "in" clause using LINQ RRS feed

  • Question

  • Hi,

    Can anyone please suggest me the linq query for the below mentioned SQL query. It looks silly but I am a beginner on LINQ-SQL. Please suggest.

        select * from Product.CastStatus WHERE CastStatusID IN ('4','5','6','7')

    Regards

    -pep

    Tuesday, February 7, 2012 11:59 AM

Answers

  • Hi,

    You can use Contains method - CastStatusID.Contains()

    Tuesday, February 7, 2012 12:11 PM
  • from product in db.Product.CastStatus
    where
      (new string[] {"4", "5", "6", "7" }).Contains(product.CastStatus.CastStatusID)
    select new {
      product.CastStatus.CastStatusID,
      product.CastStatus.CategoryName
    }

    There are some things strange here but still hope it helps.

    To learn linq, linqpad is a great help http://www.linqpad.net/

    Also te learn, there is a trial available of Linqer that can convert simple TSQL to Linq http://www.sqltolinq.com/help

    Hope this helps and good luck !


    Tuesday, February 7, 2012 12:40 PM
  • Hi pepcoder;

    This should give you the wanted results.

    List<string> statusID = new List<string>() {"4","5","6","7"};
    
    var results = from cs in Product.CastStatus
                  where statusID.Contains(cs.CastStatusID)
                  select cs;


    Fernando (MCSD)

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

    Tuesday, February 7, 2012 8:20 PM

All replies

  • Hi,

    You can use Contains method - CastStatusID.Contains()

    Tuesday, February 7, 2012 12:11 PM
  • from product in db.Product.CastStatus
    where
      (new string[] {"4", "5", "6", "7" }).Contains(product.CastStatus.CastStatusID)
    select new {
      product.CastStatus.CastStatusID,
      product.CastStatus.CategoryName
    }

    There are some things strange here but still hope it helps.

    To learn linq, linqpad is a great help http://www.linqpad.net/

    Also te learn, there is a trial available of Linqer that can convert simple TSQL to Linq http://www.sqltolinq.com/help

    Hope this helps and good luck !


    Tuesday, February 7, 2012 12:40 PM
  • Hi pepcoder;

    This should give you the wanted results.

    List<string> statusID = new List<string>() {"4","5","6","7"};
    
    var results = from cs in Product.CastStatus
                  where statusID.Contains(cs.CastStatusID)
                  select cs;


    Fernando (MCSD)

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

    Tuesday, February 7, 2012 8:20 PM
  • Thanks Fernando for the quick help. It works!!!
    Wednesday, February 8, 2012 4:10 AM

  • Thanks KeesDijk for the quick help. It works!!!
    Wednesday, February 8, 2012 4:10 AM
  • Hi,

    You can use Contains method - CastStatusID.Contains()


    Thanks Gambe...I have used the same..thanks again!!!
    Wednesday, February 8, 2012 4:11 AM