How do I best find out if specific product number exit in the list

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I have a generic list of many Products like
List<Product> list = new <Product>()

This product class contain several fields
but one field is called prodnr and its property ProdNr.

If I now want to find ouf if prodnr = 37 exist in the list
how do I best do that.

I could loop but it must be a better way.

Is it possibloe to use contains in some way.

//Tony
 
Tony said:
I have a generic list of many Products like
List<Product> list = new <Product>()

This product class contain several fields
but one field is called prodnr and its property ProdNr.

If I now want to find ouf if prodnr = 37 exist in the list
how do I best do that.

list.Any(p => p.ProdNr == 37)
is one way, assuming it is .NET 3.5 with LINQ.
 
Tony said:
I have a generic list of many Products like
List<Product> list = new <Product>()

This product class contain several fields
but one field is called prodnr and its property ProdNr.

If I now want to find ouf if prodnr = 37 exist in the list
how do I best do that.

list.Any(p => p.ProdNr == 37)
is one way, assuming it is .NET 3.5 with LINQ.
 
This is one way using delegate

int pos = list.FindIndex(delegate(Product p){ return p.ProdNr == 37; });
 
This is one way using delegate

int pos = list.FindIndex(delegate(Product p){ return p.ProdNr == 37; });
 
Back
Top