2009-04-21 61 views
3

LINQ到對象中搜索 - 如果列表中搜索LINQ到對象 - 如果列表

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(); 

回答

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