Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding a match in List<T>

Posted on 2013-01-14
3
Medium Priority
?
285 Views
Last Modified: 2013-01-18
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
Comment
Question by:countrymeister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 total points
ID: 38775465
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
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 1600 total points
ID: 38776953
You can delegate a function to find item like:
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38793015
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question