Nick LaMarca
Nick LaMarca

Reputation: 8188

Using IQueryable Filtering on a custom collection

I am trying to use LINQ to replace the following code..

public List<LineItem> GetEULineItems(PurchaseOrder p)
    {
        List<LineItem> EULineItems = new List<LineItem>();

            foreach (LineItem li in p.OrderForms[0].LineItems)
            {
                if (li[Constants.ProductSource] != null)
                {
                    if (li[Constants.ProductSource].ToString().Trim() == "EU")
                    {
                        EULineItems.Add(li);
                    }
                }
            }

        return EULineItems;
    }

I tried this but am gettin an exception..

 IQueryable<LineItem> EULineItems = p.OrderForms[0].LineItems.AsQueryable().Where(s => s.ProductSource == "EU");

Exception: 'System.Linq.IQueryable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 148

Answers (1)

BrokenGlass
BrokenGlass

Reputation: 160962

Assuming you are in fact using a custom collection you are not looking for an IQueryable, you are looking for an IEnumerable, and that should work right out of the box:

IEnumerable<LineItem> EULineItems = p.OrderForms[0]
                                     .LineItems
                                     .Where(s => s.ProductSource == "EU");

(provided of course you have a using System.Linq in your code file).

Upvotes: 1

Related Questions