• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 648
  • Last Modified:

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?
0
copyPasteGhost
Asked:
copyPasteGhost
  • 4
  • 2
1 Solution
 
Fernando SotoCommented:
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 VengalaCommented:
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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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
 
Sreedhar VengalaCommented:
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
 
copyPasteGhostAuthor Commented:
cool I'll try it and get back to you.
0
 
copyPasteGhostAuthor Commented:
Thanks.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now