LINQ表达式中的In子句 [英] In clause in linq expression

查看:175
本文介绍了LINQ表达式中的In子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在线测试应用程序,该应用程序具有两个表类别和子类别,我希望借助类别和子类别从中选择一些问题.类似于(问题(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);

这将在幕后生成IN语句.您将要确保数组相对较小,以免在

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆