Creating generic method

Can someone please help with the following... it's driving me nuts...

// Three methods, virtually identical with the exception of the select field
public IEnumerable<int> GetBrandID()
{
    return myData.Select(m => m.BrandID).Distinct();
}
public IEnumerable<int> GetModelID()
{
    return myData.Select(m => m.ModelID).Distinct();
}
public IEnumerable<int> GetVehicleID()
{
    return myData.Select(m => m.VehicleID).Distinct();
}

// How to create one method which returns the type specified in the parameter??
public IEnumerable<int> GetData(??? myType)
{
    return myData.Select(m => myType).Distinct();
}
Jon Skeet
people
quotationmark

It sounds like you probably just want a Func<Model, int> parameter:

public IEnumerable<int> GetData(Func<Model, int> projection)
{
    return myData.Select(projection).Distinct();
}

You could then have:

var modelIds = GetData(m => m.ModelID);
var vehicleIds = GetData(m => m.VehicleID);

Is that what you're after? (That's assuming myData is an IEnumerable<Model>. If it's an IQueryable<Model> you may want to accept Expression<Func<Model, int>> instead.)

people

See more on this question at Stackoverflow