LINQ表达式中的In子句 [英] In clause in linq expression
问题描述
我正在开发一个在线测试应用程序,该应用程序具有两个表类别和子类别,我希望借助类别和子类别从中选择一些问题.类似于(问题(1)中的类别ID和(1,2,3,4)中的subcategoryID
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);
或subcategoryID.contains("some value in array of integer")
我可以从任何人那里得到一些帮助吗?
can i get some help from anybody?
推荐答案
您可以按照 Linq到实体-SQL"IN";条款.
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.
这篇关于LINQ表达式中的In子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!