我正在开发一个在线测试应用程序,其中有两个表类别和子类别,我希望在类别和子类别的帮助下从中选择一些问题。类似的问题(问题在(1)中的类别ID和(1,2,3,4)
中的子类别ID)我将获得将在查询中传递的子类别列表
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);
或类似subcategoryID.contains("some value in array of integer")
我可以从任何人那里得到一些帮助吗?
答案 0 :(得分:3)
您可以根据https://blogs.msdn.microsoft.com/alexj/2009/03/25/tip-8-how-to-write-where-in-style-queries-using-linq-to-entities/和Linq to Entities - SQL "IN" clause使用Contains
。
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);
这将在幕后生成IN
语句。你需要确保阵列相对较小,这样你就不会耗尽SQL Server parameter limit。