0

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();
}
1
  • One pirate asked another, "Why is that ship wheel sticking out of your pants?"... Commented Apr 28, 2016 at 16:52

3 Answers 3

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for the assistance, it was clear and straight to the point and helped me immensely. Much appreciated. X22.
1

It's not clear what you're exactly after. Maybe something like this?

public static IEnumerable<TResult> GetData<TModel, TResult> (this IEnumerable<TModel> enumerable, Func<TModel, TResult> projection)
{
    return enumerable.Select(projection);
}

And than just call like this:

var ints = myData.GetData<MyModel,int>(m=>m.ModelID).Distinct();
var doubles = myData.GetData<MyModel,double>(m=>m.DoubleProp).Distinct();

etc...

1 Comment

Note that if you're providing the data as well, there's no benefit in adding the method - var ints = myData.Select(m => m.ModelID).Distinct()...
0
public ICollection<T> FindAllWhere(Expression<Func<T, bool>> selectSelector)
{
    ICollection<T> elements = EntitySet.Select(selectSelector).ToList().Distinct();

    return elements;
}

3 Comments

This doesn't appear to answer the question, IMO... it looks like it's an answer to a different question.
It`s more generic and can change event type of int
Well no, at the moment it wouldn't even compile. Let's start with something that answer's the OP's actual requirements, before trying to make it more general than taht.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.