我有两个这样的类(实体框架,代码优先):
public class Order
{
[Id and other properties]
public ICollection<Item> Items { get; set; }
}
public class Item
{
[Id and other properties]
public String ItemCode { get; set; }
}
我想要的是包含特定ItemCode(例如ItemCode ==“ pasta”)的订单列表
TIA
答案 0 :(得分:0)
List<Order> orderList = new List<Order>();
List<Item> itemList = new List<Item>();
itemList .add(new Item() {
ItemCode = "pasta",
});
itemList .add(new Item() {
ItemCode = "not pasta",
});
orderList.First().Items = itemList;
var x = orderList.Where(order => order.Items.Any(item => item.ItemCode == "pasta")).ToList();
答案 1 :(得分:0)
感谢Zeta:您就在附近
正确的解决方案是:
List<Order> orders = new List<Order>();
[populate orders list]
var myList = orders.Where(o => o.Items.Any(i => i.ItemCode == "pasta")).ToList();
(您在“ Any”之前忘记了“项目”) 非常感谢