简化Linq以返回View Model

时间:2014-08-01 05:20:47

标签: linq

如何简化以下声明?

var Orders = db.Orders
               .Include(o => o.shipment)
               .Where(o => o.ID == 3 || o.ID == 5 || o.ID == 10)
               .ToList();

最终,我想这样做:

SELECT * FROM Orders WHERE ID IN (3,5,10)

在强类型实体中返回View模型

我已尝试过.Any.Contains.Intersect但不能"形成" Linq查询。

2 个答案:

答案 0 :(得分:4)

这样的东西?

var items = new int[] {3,5,10};
var Orders = db.Orders
               .Include(o => o.shipment)
               .Where(o => items.Contains(o.ID))
               .ToList();

答案 1 :(得分:1)

这个怎么样?

List<int> items = new List<int>(new []{3,5,10});

var Orders = db.Orders
               .Include(o => o.shipment)
               .Where(o => items.Contains(o.ID))
               .ToList();

代替int,您应该使用与ID相同的数据类型

希望它有所帮助!!!