如何在LinqToSql中执行WHERE ... IN ...子句? [英] How to do a WHERE...IN... clause in LinqToSql?
问题描述
忍受我,我开始了:如何使用WHERE ... IN ...类型的子句选择多个元素
Bear with me, I'm beginning: How can I select multiple elements using a WHERE...IN... type of clause as in
select * from orders where orderid in (1, 4, 5)
在LinqToSql中?我宁愿不要有lambda表情,因为它们会吓到我.预先感谢!
in LinqToSql? I'd prefer not to have a lambda expression since they scare me. Thanks in advance!
推荐答案
LINQ具有"Contains",类似于"IN",但是却以相反的方式表示-元素不在集合中,而集合包含" 一个元素.
LINQ has "Contains" which is like "IN" but expressed the other way round - an element isn't "in" a set, a set "contains" an element.
int[] validIds = { 1, 4, 5 };
var query = from order in db.Orders
where validIds.Contains(order.Id)
select order
这是用lambda更简单地表示(IMO):
This is more simply expressed (IMO) with a lambda though:
int[] validIds = { 1, 4, 5 };
var query = db.Orders.Where(order => validIds.Contains(order.Id));
我意识到lambda是新的",因此在某种程度上令人恐惧,但是用双手抓住它们确实很值得.他们很可爱.
I realise lambdas are "new" and therefore scary to some extent, but it's really well worth grabbing hold of them with both hands. They're lovely.
这篇关于如何在LinqToSql中执行WHERE ... IN ...子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!