LeedsWalker
LeedsWalker

Reputation: 199

C# and delegates

I want to use a delegate which allows me to seach for a match on one item in a list with another item in the other list by using FIND.

I have got the code working as follows:-

Type a = listname.Find(delegate(Type b))
{
         return list.item == b.list.item;
}

If the list item matches the item in b, then it returns the item in a. This is working fine, however I now want to check that I only return a if more than one item in list.item matches b.list.item such as

Type a = listname.Find(delegate(Type b))
{
         return list.item == b.list.item;
                 list.anotheritem == b.list.anotheritem
}

I can't seem to code it so that it checks for more than one condition before returning Type a.

Upvotes: 1

Views: 270

Answers (4)

Glory Raj
Glory Raj

Reputation: 17701

Type a = listname.Find(delegate(Type b) 
{ 
         return list.item == b.list.item &&
                 list.anotheritem == b.list.anotheritem;
} 

Upvotes: 3

Adam Lear
Adam Lear

Reputation: 38778

You need an AND operator instead of a semi-colon to combine the two conditions:

Type a = listname.Find(delegate(Type b)
{
         return list.item == b.list.item 
                && list.anotheritem == b.list.anotheritem;
}

Upvotes: 2

Joe
Joe

Reputation: 82654

     return list.item == b.list.item;
             list.anotheritem == b.list.anotheritem

should be

     return list.item == b.list.item &&
             list.anotheritem == b.list.anotheritem;

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 942448

Use the logical AND operator, &&.

     return list.item == b.list.item && 
            list.anotheritem == b.list.anotheritem

Upvotes: 2

Related Questions