How to use predicate

J

jyothi

Hi All,

when i buid this code i get an error saying the syntax is incorrect to use
the delegate .w.r.t anonymous method. Upon more digging i found that i need
to define the delegate method.
The method i need to pass to the delegate needs two parameters i.e the
customer object and the id to check. I'm stuck how to pass 2 parameters to
the method.

Can somebody explain me how to do it?

//C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public class CustomerList
{
private static List<Customer> custList = new List<Customer>();

public List<Customer> Select()
{
return custList;
}

public Customer SelectSingle(int selectCustomerId)
{
if (selectCustomerId == -1) return null;
return custList.Find(
delegate(Customer customer)
{ return customer.Id == selectCustomerId; });
}

public void Update(Customer updateCustomer )
{
Customer customerFound = custList.Find(
delegate(Customer customer)
{ return customer.Id == updateCustomer.Id; });
customerFound.Id = updateCustomer.Id;
customerFound.Name = updateCustomer.Name;
customerFound.City = updateCustomer.City;
customerFound.State = updateCustomer.State;
customerFound.Phone = updateCustomer.Phone;
}

public void Insert(Customer _customer)
{
custList.Add(_customer);
}

public void Delete(Customer deleteCustomer)
{
Customer customerFound = custList.Find(
delegate(Customer customer)
{ return customer.Id == deleteCustomer.Id; } );
custList.Remove(customerFound);
}
}
 
B

bruce barker

its unclear what your question is. you don't say which line is the
error, nor do any of the delegates have two args. also if you are using
3.5, lambda expressions are simpler:

public Customer SelectSingle(int selectCustomerId)
{
if (selectCustomerId == -1) return null;
return custList.Find(c => c.id == selectCustomerId);
}

-- bruce (sqlwork.com)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top