468,110 Members | 1,562 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

List.Exists Predicate

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
5 54513
John Cantley <jo****@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 - <sk***@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
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" <jo****@magenic.comwrote in message
news:On**************@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
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
John Cantley <jo****@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 - <sk***@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
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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Moosebumps | last post: by
1 post views Thread by maxtoroq | last post: by
2 posts views Thread by Tarscher | last post: by
7 posts views Thread by xianwei | last post: by
reply views Thread by Terry Reedy | last post: by
36 posts views Thread by pereges | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.