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<int> items;
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

Re: Help with List<>..Find() by zacks

zacks
Wed May 07 08:24:17 CDT 2008

On May 7, 9:17=A0am, Phil <phi...@sbcglobal.net> wrote:
> I have seen the samples for Find that explain how to use the predicate, bu=
t
> they are always searching for a pre-defined value. =A0What I don't underst=
and
> is how to search for a random value stored in a variable.
>
> For example,
>
> List<int> items;
> int val=3D3;
>
> 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.

Re: Help with List<>..Find() by parez

parez
Wed May 07 08:26:31 CDT 2008

On May 7, 9:17 am, Phil <phi...@sbcglobal.net> 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.
>
> For example,
>
> List<int> items;
> 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; });

Re: Help with List<>..Find() by Jon

Jon
Wed May 07 08:33:09 CDT 2008

On May 7, 2:26 pm, parez <psaw...@gmail.com> wrote:
> int b = items.Find(delegate(int a) { return a == val; });

Or in C# 3:

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

Jon

Re: Help with List<>..Find() by sloan

sloan
Wed May 07 08:35:54 CDT 2008


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" <phil78@sbcglobal.net> wrote in message
news:B1EE89D2-7DD5-41AF-8A90-EB4D98E02510@microsoft.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<int> items;
> 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



Re: Help with List<>..Find() by Leo

Leo
Wed May 07 08:33:26 CDT 2008

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" <phil78@sbcglobal.net> wrote in message
news:B1EE89D2-7DD5-41AF-8A90-EB4D98E02510@microsoft.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<int> items;
> 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


Re: Help with List<>..Find() by Ben

Ben
Thu May 08 15:44:31 CDT 2008

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?