0

i am developing a online test application where have two tables category and subCategory from which i want to select some questions with the help of category and subcategory. something like ( question where category ID in (1) and subcategoryID in (1,2,3,4)

I am getting list of subcategory that is going to pass in query

int[] subCategoryForQuestions=new int[]{1,2,3,4};

var TestDataList = coreDbContext.SolutionMasters
                       .Where(x => x.CategoryID == categoryID
                                   && x.DifficultyId == questionLevel
                                   && subCategoryForQuestions.Contains("here all value in Subcategory"))
                      .Take(NoOfQuestions);

or something like subcategoryID.contains("some value in array of integer")

can i get some help from anybody?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • Possible duplicate of [Linq to Entities - SQL "IN" clause](https://stackoverflow.com/questions/857973/linq-to-entities-sql-in-clause) – mjwills Jul 08 '17 at 23:13

1 Answers1

3

You could use Contains as per https://blogs.msdn.microsoft.com/alexj/2009/03/25/tip-8-how-to-write-where-in-style-queries-using-linq-to-entities/ and Linq to Entities - SQL "IN" clause .

int[] subCategoryForQuestions=new int[]{1,2,3,4};

var TestDataList = coreDbContext.SolutionMasters
                       .Where(x => x.CategoryID == categoryID
                                   && x.DifficultyId == questionLevel
                                   && subCategoryForQuestions.Contains(x.subcategoryID))
                      .Take(NoOfQuestions);

This will generate an IN statement behind the scenes. You will want to ensure that the array is relatively small so you don't exhaust the SQL Server parameter limit.

mjwills
  • 23,389
  • 6
  • 40
  • 63