[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Finding a match in List<T>

I have a object O which has four properties a, b, c and d, I have a list List<T>.
I need to find the match in List<T> which matches the values of a and b in Object O

The object O could have a match in List<T>, but not always.
Trying to do this in linq, or whichever is most effective from a performance point.
List<T> could have around 1000 records.
0
countrymeister
Asked:
countrymeister
  • 2
2 Solutions
 
AndyAinscowCommented:
Use the Find with prediacte functionality of a List eg.

List<T> sublist = masterlist.FindAll(foo);


private static bool foo(T obj)
{
//  return true if this is an object you require else return false
}
0
 
Minh Võ CôngCommented:
You can delegate a function to find item like:
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
0
 
AndyAinscowCommented:
I'm a touch surprised at the choice you made.
The comment by minhvc is effectively a copy of my comment but differing in one important aspect.  That only returns one item at most, as you wanted to select on a subset of the properties I thought the FindAll would be rather more suitable which returns more than one item if there were multiple matches in the list.  Of course for just one match or none the return results of both methods would be the same.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now