Xtremcool Xtremcool - 1 year ago 94
ASP.NET (C#) Question

Get IList<> detail again filtered

Hi I get the data in products from below query but i again want products to get filtered and store that data in another Ilist name product1. I had written below code but it snot working, can any bodyu help me out?

IList<Product> products = ProductDataSource.FindProducts();//store data in list of products

IList<Product> products1 = products.SingleOrDefault(s => s.Visibility == 2));

It will be good enough if Ilist product get filtered and the filtered detail remain in itself only.
So that there will be no need to declare one more Ilist as product1

Answer Source

Linq method SingleOrDefault does not produce a List, it produces a single item (or null).

So it should be:

Product product = products.SingleOrDefault(s => s.Visibility == 2);

Or if you do need a List:

IList<Product> products1 = products.Where(s => s.Visibility == 2).ToList(); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download