473,385 Members | 1,925 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,385 software developers and data experts.

Using List.Exists Method With Predicate

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
a match. If a match is found, I need to move the matched item into
another list.

I want to compare the objects by value. The items in each list have an
ID property (of type object) which I want to convert to a strng in
order to be able to do this.

I cannot use the Contains method to perform this check. Contains has
been overridden. I also run the risk of breaking the control (which
someone else wrote) if I modify this behavior.

I hope the above explanation makes sense.

I know that I can create a method which will be used as a predicate to
check for the specified condition (a match) but I have been unable to
get the syntax right.

Does anyone have some idea of how I might go about this? Any help
(particularly example code) would be much appreciated.

Jul 5 '06 #1
2 7144
Try the List<T>.Find method.
http://msdn2.microsoft.com/en-us/library/x0b5b5bc.aspx You can use a method
shown in the sample code or an anonymous delegate.

Predicate<Tmatch = delegate(T item)
{
return item.Id == REQUIRED_ID;
};

T found = list.Find(match);

if (found != null)
{
//move/copy found to list2
}

"sianan" <sm*********@tds.netwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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
a match. If a match is found, I need to move the matched item into
another list.

I want to compare the objects by value. The items in each list have an
ID property (of type object) which I want to convert to a strng in
order to be able to do this.

I cannot use the Contains method to perform this check. Contains has
been overridden. I also run the risk of breaking the control (which
someone else wrote) if I modify this behavior.

I hope the above explanation makes sense.

I know that I can create a method which will be used as a predicate to
check for the specified condition (a match) but I have been unable to
get the syntax right.

Does anyone have some idea of how I might go about this? Any help
(particularly example code) would be much appreciated.

Jul 5 '06 #2
"sianan" <sm*********@tds.netwrote:
(Title: Using List.Exists Method With Predicate)

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
a match. If a match is found, I need to move the matched item into
another list.

I want to compare the objects by value. The items in each list have an
ID property (of type object) which I want to convert to a strng in
order to be able to do this.
So, you mean something like:

---8<---
// Word of warning: this takes time proportional to n*m
List<YourTypelistToDoStuffWith = list.FindAll(delegate(YourType value)
{
return otherList.Exists(delegate(YourType otherValue)
{
// Maybe use some other string.Equals() overload here
return value.ID.ToString() == otherValue.ID.ToString();
});
});

list.RemoveAll(listToDoStuffWith.Contains);
// or alternatively:
listToDoStuffWith.ForEach(list.Remove);

listToAddTo.AddRange(listToDoStuffWith);
--->8---
I cannot use the Contains method to perform this check. Contains has
been overridden.
The Contains method of List<Tcannot be overridden. It can only be
hidden with a new definition.
I know that I can create a method which will be used as a predicate to
check for the specified condition (a match) but I have been unable to
get the syntax right.
You can create a static or instance method with the same signature as
the anonymous delegates I used above (i.e. taking one argument of your
list type and returning a boolean value). I personally think anonymous
delegates are easier to read, because otherwise you end up with a method
that is textually distant from where it is used, yet intrinsically
linked with the implementation of the body of the calling method.

It makes sense to move from an anonymous delegate to a separate method
if it becomes possible to reuse the predicate.

-- Barry

--
http://barrkel.blogspot.com/
Jul 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Richardson | last post by:
I am attempting to copy some files from one machine to another. I am having difficulty with the File.Exists() in the code below. The program enumerates the files in a directory, checks them...
0
by: softengine | last post by:
I have a dataset with multiple tables I need exported to Excel. I can get it into Excel into one worksheet however I want there to be one worksheet for each datatable in the dataset. If I dump my...
14
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I...
0
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
4
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or...
1
by: BobAchgill | last post by:
Is there a special method to find out if a folder exists? The File.exists method for some reason did not work for me to see a folder. So I thought maybe there is another way to see if a folder...
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...
5
by: John Cantley | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.