473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I use the Find method on a generic List collection

Hello!

Assume I have the class shown below and it's stored in a generic List.
If I want to find the Value for a specific Id how do I use the find method.
We can assume that the generic List collection is named
myWorkSheetRowParameterList

public class WorkSheetRowParameter
{
public string WorkSheetRowId {get; set};
public string Id {get;set};l
public string Type {get;set};
public string Value {get;set};
public string Disabled{get;set};
public string ParameterName{get;set};
public string ParameterDuplicate{get;set};
}

//Tony
Sep 22 '08 #1
3 2133
You need a "predicate" - i.e. a condition to match, expressed as a
delegate. Since you are using C# 3.0, this is simply:

string id = "abc"; // etc
WorkSheetRowParameter row =
myWorkSheetRowParameterList.Find(
x =x.Id == id);

Marc
Sep 22 '08 #2
oh, I forgot - you'll need to get .Value afterwards.

Actually, with LINQ (in .NET 3.5) you can also do:

string id = "abc"; // etc
string value = (from row in myWorkSheetRowParameterList
where row.Id == id
select row.Value).Single();

Marc
Sep 22 '08 #3
Tony Johansson <t.*********@logica.comwrote:
Assume I have the class shown below and it's stored in a generic List.
If I want to find the Value for a specific Id how do I use the find method.
We can assume that the generic List collection is named
myWorkSheetRowParameterList

public class WorkSheetRowParameter
{
public string WorkSheetRowId {get; set};
public string Id {get;set};l
public string Type {get;set};
public string Value {get;set};
public string Disabled{get;set};
public string ParameterName{get;set};
public string ParameterDuplicate{get;set};
}
It looks like you're using C# 3, so use a lambda expression:

string id = // the ID you want to find.
var found = myWorkSheetRowParameterList.Find(p =p.Id == id);
string value = found.Value;

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
3
by: snesbit | last post by:
I have a structure called SearchAreaListItem. The structure has some properties. The application implements this as a collection.generic.list(of SearchAreaListItem) I load the collection up ...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
6
by: Lee Crabtree | last post by:
Pinning a regular managed array is pretty easy in C++/CLI, such as: if buffer is defined thusly: array<System::Byte^buffer pinning it would be something like: pin_ptr<unsigned charpinBuf =...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
30
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
3
by: Fresno Bob | last post by:
I have a generic collection of objects and I would like to find the object by one of it's properties e.g. I would like something with the functionality of something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.