I wondering if someone may be able to help me, I have two List T
in C#, They are the same apart from one holds customers and one holds vendors. I am trying to pass each one of these lists into a function with no success to avoid having extra code.
Lists created below:
var customers = IntuitServiceConfig.ServiceManager.FindAll<Customer>(new Customer(), 1, 500);
var vendor = IntuitServiceConfig.ServiceManager.FindAll<Vendor>(new Vendor(), 1, 500);
I would like to pass into the function below:
public static string Contacts(AppContext ctx, List<Type1> list )
{
foreach (var contact in list)
{
// do some db work
}
}
If you want/can use same code for both classes(Customer
, Vendor
), then change your Contacts
function to generic
public static string Contacts<TContact>(AppContext ctx, List<TContact> list )
{
foreach (var contact in list)
{
// do some db work
}
}
Then use like this:
List<Customer> customers = IntuitServiceConfig.ServiceManager.FindAll(new Customer(), 1, 500).ToList();
List<Vendor> vendors = IntuitServiceConfig.ServiceManager.FindAll(new Vendor(), 1, 500).ToList();
String cust = Contacts<Customer>(ctx, customers);
String vend = Contacts<Vendor>(ctx, vendors);
UPDATE
If your Vendor
and Customer
classes are different, not derived from same base class or haven't implemented same interface, than you need write own function for every class
public static string Contacts(AppContext ctx, List<Customer> list )
{
foreach (var contact in list)
{
// do some db work with customers
}
}
public static string Contacts(AppContext ctx, List<Vendor> list )
{
foreach (var contact in list)
{
// do some db work with vendors
}
}
And using:
String cust = Contacts(ctx, customers);
String vend = Contacts(ctx, vendors);