Linq to Objects - Where search within a list - Easy one...

I'm trying to do something like this:

int warehouseID = 1;

List<oProd> FilteredProdList =
            (from c in queryList
             where c.ListOfProdQtyByWarehouse.Contains(new ProdWarehouseQty {
                WarehouseID = warehouseID
             })
             select c).ToList();

return FilteredProdList ;

The queryList object contains a list of type ProdWarehouseQty one of the members of ProdWarehouseQty is "WarehouseID" this is an int field.

I want to return all the objects in queryList that match the given warehouse id...

I think i'm making this harder than it should be...

any ideas?
LVL 13
copyPasteGhostAsked:
Who is Participating?
 
Sreedhar VengalaConnect With a Mentor Sr. Consultant - Business IntelligenceCommented:
Yes that should work fine ( as below)
  int id = 4;
            List<ProdQtyByWarehouse> list = (from c in prodQtyByWarehouses 
                                             where c.ProdWarehouseQtys.Exists(x => id == x.id) select c).ToList();

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi copyPasteGhost;

It is hard to give an answer without knowing the structure of some of the object. For example you want the query to return a List of oProd, how is oProd constructed?

You state that c.ListOfProdQtyByWarehouse seems to be a member of the object ProdWarehouseQty, can you show how ProdWarehouseQty is constructed?

Fernando
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
Here's a sample code, might fit your requirements:

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Linq;
 
namespace ExpExe_Engine_WEB
{
    public partial class _Default : Page
    {
        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> { 3, 4, 45, 53, 55 };
            List<ProdQtyByWarehouse> list = (from c in prodQtyByWarehouses 
                                             where c.ProdWarehouseQtys.Exists(x => integers.Contains(x.id)) select c).ToList();
        }   
    }   
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
copyPasteGhostAuthor Commented:
public class ProdWarehouseQty{
      public Guid ProdGuidID { get; set; }
      public decimal QtyOnHand { get; set; }
      public int WarehouseID { get; set; }
}

 public class ProdCol : CollectionBase {
      public string Name { get; set; }
      public Guid GuidID { get; set; }
      public List<ProdWarehouseQty> ListOfProdQtyByWarehouse{ get; set; }
      public decimal TotalQtyAval { get; set; }
}
0
 
copyPasteGhostAuthor Commented:
@sree_ven

I modified your code a little:

List<Prod> list = (from c in oProdCol
                               where c.ListOfProdQtyByWarehouse.Exists(x => GetIdWarehousesAvailable().Contains(x.WarehouseID))
                                          select c).ToList();

This will return me all the warehouses since I'm passing that as my list of ints. BUT I would like to only retrieve results from a specific warehouse id... can I do something like this?


public static List<Prod> FilterByWarehouseID(List<Prod> oProdCol, int warehouseID) {
         
         List<Prod> list = (from c in oProdCol
                               where c.ListOfProdQtyByWarehouse.Exists(x => warehouseID == x.WarehouseID)
                                  select c).ToList();
return list;
}
0
 
copyPasteGhostAuthor Commented:
cool I'll try it and get back to you.
0
 
copyPasteGhostAuthor Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.