364,033 Members | 4865 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

List.Exists Predicate

John Cantley
P: n/a
John Cantley
How do I use this, the example in msdn is basically worthless unless you
want to check something against a static string or something. I need to
check in my list if there is a duplicate entry, how do I do that?

I have a collection of supplier objects.

Supplier
Name
ID
Count
Score

I want to do
if (suppliers.Exist(CheckNewName))
do something
else
do some other thing

private static bool CheckNewName()
{
how do i check for dups here
}

I tried doing it like a compare but that doesn't work. Any ideas?

thanks,
jc



May 8 '07 #1
Share this Question
Share on Google+
5 Replies


Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
John Cantley <johnca@magenic.comwrote:
How do I use this, the example in msdn is basically worthless unless you
want to check something against a static string or something. I need to
check in my list if there is a duplicate entry, how do I do that?
>
I have a collection of supplier objects.
>
Supplier
Name
ID
Count
Score
>
I want to do
if (suppliers.Exist(CheckNewName))
do something
else
do some other thing
>
private static bool CheckNewName()
{
how do i check for dups here
}
>
I tried doing it like a compare but that doesn't work. Any ideas?
This is where anonymous methods comes in, as demonstrated by the
following code. I've put the anonymous method on a line on its own to
make it easier to see - it doesn't have to be.

using System;
using System.Collections.Generic;

class Supplier
{
public string Name;

public Supplier (string name)
{
Name = name;
}
}

class Program
{
static void Main()
{
List<Supplierlist = new List<Supplier>();

list.Add (new Supplier("Fred"));
list.Add (new Supplier("Joe"));

Check(list, "Fred");
Check(list, "Bob");
}

static void Check(List<Supplierlist, string nameToCheck)
{
Console.WriteLine (list.Exists
(
delegate(Supplier x) { return x.Name == nameToCheck; }
));
}
}



--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #2

Troye Stonich
P: n/a
Troye Stonich
John,
You can use a Predicate<Tdelegate object to pass in to the Exists() method
of the generic list.

Here is the syntax:

if(suppliers.Exist(delegate (Supplier match)
{
return match.Name == whateverValueYouWantToMatchOn;
}))
{
do something
}
else
{
do something else
}

-Troye




"John Cantley" <johnca@magenic.comwrote in message
news:OnWRi8akHHA.2272@TK2MSFTNGP02.phx.gbl...
How do I use this, the example in msdn is basically worthless unless you
want to check something against a static string or something. I need to
check in my list if there is a duplicate entry, how do I do that?
>
I have a collection of supplier objects.
>
Supplier
Name
ID
Count
Score
>
I want to do
if (suppliers.Exist(CheckNewName))
do something
else
do some other thing
>
private static bool CheckNewName()
{
how do i check for dups here
}
>
I tried doing it like a compare but that doesn't work. Any ideas?
>
thanks,
jc
>
>
>

May 8 '07 #3

John Cantley
P: n/a
John Cantley
Thanks Jon, that worked. Here is my final function

private static bool IsNameUnique(string sNameToCheck,
SortableSuppliers<Supplierlist)
{
if (list.Exists(delegate(Supplier x) { return x.SupplierName ==
sNameToCheck; }))
return false;
else
return true;
}

john


May 8 '07 #4

Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
John Cantley <johnca@magenic.comwrote:
Thanks Jon, that worked. Here is my final function
>
private static bool IsNameUnique(string sNameToCheck,
SortableSuppliers<Supplierlist)
{
if (list.Exists(delegate(Supplier x) { return x.SupplierName ==
sNameToCheck; }))
return false;
else
return true;
}
Any reason for not just using:

return !(list.Exists(delegate(Supplier x)
{ return x.SupplierName == sNameToCheck; }))

?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #5

Frans Bouma [C# MVP]
P: n/a
Frans Bouma [C# MVP]
John Cantley wrote:
How do I use this, the example in msdn is basically worthless unless
you want to check something against a static string or something. I
need to check in my list if there is a duplicate entry, how do I do
that?
>
I have a collection of supplier objects.
>
Supplier
Name
ID
Count
Score
>
I want to do
if (suppliers.Exist(CheckNewName))
do something
else
do some other thing
>
private static bool CheckNewName()
{
how do i check for dups here
}
>
I tried doing it like a compare but that doesn't work. Any ideas?
If you want a list of unique values, don't use a list with a lookup
for every add. That's incredibly inefficient as every lookup will do a
linear search.

Simply use:
Dictionary<string, Suppliersuppliers = new Dictionary<string,
Supplier>();

and then:
if(suppliers.ContainsKey(mySupplier.Name))
{
// do something
}
else
{
// not there yet, do something else
}

this is an O(1) lookup. As string is a ref type, you might as well use
a hashtable, which is faster than a generic dictionary.

FB



--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 9 '07 #6

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp list .exists() list exist list.exists