Solved

Finding a match in List<T>

Posted on 2013-01-14
3
249 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
  • 2
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 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 400 total points
ID: 38776953
You can delegate a function to find item like:
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
0
 
LVL 44

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now