I am trying to refactor some methods to avoid repeating myself in the object mappings. The functions are laid out in this SO question.
I have a generic method that will call one of 4 stored procedures, that return results with the same fields, just different subsets of data. linq2sql generates a different Result object for each stored procedure.
Is there a way to map the results to my server object generically? Does it require reflection?
private static List<DistroHeader> getHeaders<T>(Func<IEnumerable<T>> getHeaders)
{
List<MyObj> myObj = new List<MyObj>();
var result = from a in getMyObjData()
select a;
foreach (var row in result)
{
myObj.Add(new MyObj()
{
Id = row.id,
Description = row.descr,
// ...etc
// These fields are shared across the result types...
// is there a way to make the compiler recognize that?
});
}
}
I’m guessing this isn’t good practice, but this is what I did: