How to use predicate

  • Thread starter Thread starter jyothi
  • Start date Start date
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);
}
}
 
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)
 
Back
Top