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?)
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:
(provided of course you have a
using System.Linqin your code file).