2009-04-21 9 views
3

LINQ to Objects - Où Rechercher dans une listeLINQ to Objects - Où Rechercher dans une liste

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 
    } 

Comment puis-je obtenir:

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

Répondre

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