Linq to Objects - 在列表中搜索的位置

时间:2009-04-21 08:58:28

标签: linq

Linq to Objects - 列表中的搜索

    internal class ProdQtyByWarehouse
    {
        public int id { get; set; }
        public List<ProdWarehouseQty> ProdWarehouseQtys { get; set; }
    }

    internal class ProdWarehouseQty
    {
        public int id { get; set; }
        public string PName { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var list1 = new List<ProdWarehouseQty>
                        {
                            new ProdWarehouseQty
                                {
                                    id = 3,
                                    PName = "list1PN1"
                                },
                            new ProdWarehouseQty
                                {
                                    id = 4,
                                    PName = "list1PN2"
                                }

                        };
        var list2 = new List<ProdWarehouseQty>
                        {
                            new ProdWarehouseQty
                                {
                                    id = 5,
                                    PName = "list2PN1"
                                },
                            new ProdWarehouseQty
                                {
                                    id = 6,
                                    PName = "list2PN2"
                                }

                        };
        var prodQtyByWarehouses = new List<ProdQtyByWarehouse>
                                      {
                                          new ProdQtyByWarehouse {id = 1, ProdWarehouseQtys = list1},
                                          new ProdQtyByWarehouse {id = 1, ProdWarehouseQtys = list2}

                                      };
        List<int> integers = new List<int>{2,3,4,6};

        List<ProdQtyByWarehouse> list =
            (from c in prodQtyByWarehouses
             where c.ProdWarehouseQtys.Contains(new ProdWarehouseQty {id = 3})
             select c).ToList(); // no object is returned
    }

我如何实现:

List<ProdQtyByWarehouse> list =
            (from c in prodQtyByWarehouses
             where c.ProdWarehouseQtys.Contains(new ProdWarehouseQty {id in integers})
             select c).ToList();

1 个答案:

答案 0 :(得分:3)

List<ProdQtyByWarehouse> list =
    (
        from c in prodQtyByWarehouses
        where c.ProdWarehouseQtys.Exists(x => integers.Contains(x.id))
        select c
    ).ToList();