How to check if item exists in a collection?

Posts   
 
    
w3stfa11
User
Posts: 13
Joined: 01-Jul-2010
# Posted on: 02-Jul-2010 18:17:40   

I have a ClientCollection and I'd like to check if a certain Client entity with clientid='123' exists in the collection? I don't actually need the entity, just if it exists or not. (If it returns it, that's ok, too.) Is FindMatches the correct method?

using llblgen 2.6 VS 2008

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Jul-2010 22:07:29   

Yes, FindMatches is the way to go:

IPredicate filter = (CustomerFields.CustomerId == "ALFKI");
ArrayList indexes = myCustomers.FindMatches(filter);
// found
if (indexes.Count >0)
{}

You also could use Linq2Objects:

if (myCustomers.Where(c=> c.CustomerId = "ALFKI").Count()  > 0 )
....

You also can use Filtered EntityViews

David Elizondo | LLBLGen Support Team