开发者

linq in or contains?

How can I use Linq-to-sql to a search like this:

where obj.id equals any of the following {1,2,3,4}

I would guess I could use the in or perhaps contains?

where obj.id in Enumerable.Range( (int) myEnum.Star开发者_如何学JAVAt, (int) myEnum.End) ) ?


You can use .Contains(), like this:

var list = new List<int> { 1, 2, 3, 5 };
var result = from s in DB.Something
             where list.Contains(s.Id)
             select s;

This will get translated to a parameterized form of:

WHERE Id IN (1, 2, 3, 5)


var myCustomers = new short[] {1,2,3,4};
var foo = db.Customers.Where(c=> myCustomers.Contains(c.ID));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜