List.Exists Predicate

J

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
 
J

Jon Skeet [C# MVP]

John Cantley said:
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<Supplier> list = new List<Supplier>();

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

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

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

Troye Stonich

John,
You can use a Predicate<T> delegate 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
 
J

John Cantley

Thanks Jon, that worked. Here is my final function

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

john
 
J

Jon Skeet [C# MVP]

John Cantley said:
Thanks Jon, that worked. Here is my final function

private static bool IsNameUnique(string sNameToCheck,
SortableSuppliers<Supplier> list)
{
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; }))

?
 
F

Frans Bouma [C# MVP]

John said:
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, Supplier> suppliers = 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#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top