如何在linq中使用IN运算符 [英] How to use the IN operator in linq

查看:76
本文介绍了如何在linq中使用IN运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询视图,并使用名为status的列过滤结果.我想对其进行查询,以便可以像在SQL中一样使用IN运算符来搜索状态不同的行.

I'm querying a view and filtering the results with a column named status. I'd like to query it so I can search for rows with different status, by using the IN operator as I'd do in SQL.

如此:

SELECT * FROM VIEW WHERE('....','.....')中的状态

SELECT * FROM VIEW WHERE Status in ('....', '.....')

我该如何实现?

推荐答案

如果查询表达式使用IEnumerable对象的Contains方法,则解析器将使用IEnumerable.

If your query expression uses the Contains method of an IEnumerable object, the parser will turn that into an IN expression using the values in the IEnumerable.

List<string> foo = new List<string>() { "a", "b", "c" };

var query = dataContext.View.Where(v => foo.Contains(v.Status));

这篇关于如何在linq中使用IN运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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