473,378 Members | 1,541 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,378 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 7143
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.