473,403 Members | 2,354 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,403 software developers and data experts.

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 54850
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Moosebumps | last post by:
Is there a function that takes a list and predicate, and returns two lists -- one for which the predicate is true and one for which it is false? I know I can call filter(p, list) and filter( not...
3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
1
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
2
by: Tarscher | last post by:
Hi all, I have a list of integers and simply want to check if a number exists in that list. In the manual I found the List.Exists method but this apparently only works for constants values via a...
9
by: Andreas Schmitt | last post by:
I am somewhat new to C# and I ran into a problem in a small program I am writing for teaching myself. I am handling a list ob objects and I want to delete some of them inside a loop like in: ...
7
by: xianwei | last post by:
Guys: The problem come from a book Data structures and Algorithm Analysis in C First, some code in list.h struct Node; typedef struct Node Node; typedef struct Node *PtrNode;...
0
by: Terry Reedy | last post by:
"Jared Grubb" <jared.grubb@gmail.comwrote in message news:925822270804230959v557ec5f5re02737709f94d3c6@mail.gmail.com... | I want a function that removes values from a list if a predicate...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.