473,385 Members | 1,798 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,385 software developers and data experts.

Help with List<>..Find()

I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
Jun 27 '08 #1
6 2664
On May 7, 9:17*am, Phil <phi...@sbcglobal.netwrote:
I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. *What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
One way is to use the .Contains method, but it will only find the
first one. There is also .FindExactString method if the list is a list
of strings.
Jun 27 '08 #2
On May 7, 9:17 am, Phil <phi...@sbcglobal.netwrote:
I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
int b = items.Find(delegate(int a) { return a == val; });
Jun 27 '08 #3
On May 7, 2:26 pm, parez <psaw...@gmail.comwrote:
int b = items.Find(delegate(int a) { return a == val; });
Or in C# 3:

int b = items.Find(a =a==val);

Jon
Jun 27 '08 #4

If you're only concerned with int's..you have an answer already.

If you're interested in this .. in general..I'd suggest:
http://ludwig-stuyck.spaces.live.com/Lists/cns!E36D9BA98FC913B3!296/

Find the C# Generics article (in english) and bookmark and read that.

...

"Phil" <ph****@sbcglobal.netwrote in message
news:B1**********************************@microsof t.com...
>I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't
understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil

Jun 27 '08 #5
Try:
items.Select(i =i == val);
or
items.Find(i =i == val);
or
items.Where(i =i == val);

depending on what you mean by Find... refer to
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Regards,
Leo

"Phil" <ph****@sbcglobal.netwrote in message
news:B1**********************************@microsof t.com...
>I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't
understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
Jun 27 '08 #6
Phil wrote:
I have seen the samples for Find that explain how to use the
predicate, but they are always searching for a pre-defined value.
What I don't understand is how to search for a random value stored in
a variable.
Are you sure you don't want IndexOf instead of Find?
Jun 27 '08 #7

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

Similar topics

4
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<>...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
0
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
3
by: muquaddim | last post by:
Hello, I have a xml file like the following. <?xml version="1.0"> <data> <idef units="Vin,Vout,E"> <i id="i1"> <sample num="1"> <sampledata value="2;3;7" /> </sample>
11
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
4
by: =?Utf-8?B?SkI=?= | last post by:
Hello List<Tis said to be more powerful than ArrayLists but if you have something like this: List<intmylst = new List<>; myList.Add("Joe"); myList.Add(25); the list doesn't seem to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.