Everyone projects once in a while:
var c = dc.Products.Select( p => new {p.id, p.name});
// results in c having properties of .id and .name
What if I want to refactor that in to a method. How would I pass the Select parameter?
var c = myFunction( p => new {p.id, p.name});
Product myFunction( ??? myLambda)
{
var c = dc.Products.Select( myLambda);
var result = new Product();
foreach( var myproperty in //property in myLambda)
{
result.XX // property that matches, set it equal to myproperty
}
}
Obviously, I need to figure out reflection a lot more. 🙂
The type of the parameter should be something like
Expression<Func<Product,object>>. Also, I think you need your function to return anIEnumerable<Product>with the properties set.Having said that I think I would probably have a separate model for each combination of properties being returned and use a generic method to return one of those.
used as
Note: After thinking about it a little, if you are going to return a separate model, I think you may need to realize the query before you can do the conversion to the model anyway. In that case a
Func<Product,T>is probably most appropriate — I’ve thrown an explicit ToList() in to make sure that the query is realized first so the select doesn’t fail. If it were aWhereclause, that would be different. Even though this is slightly less efficient from a data transfer perspective (you’re retrieving all the properties), I still prefer using using the specific model if you feel like you need to put the selection in the repository. Since you’re only selecting part of the properties it feels wrong to pretend that you have an actual Product instance when you really don’t.