I have a generic func that takes a string of fields and selects them from an object using linq. However the output is not type of object but a subset of. So to convert this new List to a datatable, I cannot use list.GetType() because it will get me Original.
I have converted Func CreateSelectStatement(string fields) to Func CreateSelectStatement(string fields) because I don't want TResult to be of type T. Not sure if this is the correct way. This is the code I have at the moment and how it is invoked.
Func<T, S> CreateSelectStatement<T, S>(string fields)
{
var xParameter = Expression.Parameter(typeof(T), "o");
var xNew = Expression.New(typeof(T));
var bindings = fields.Split(',').Select(o => o.Trim())
.Select(o => {
var mi = typeof(T).GetProperty(o);
var xOriginal = Expression.Property(xParameter, mi);
return Expression.Bind(mi, xOriginal);
}
);
var xInit = Expression.MemberInit(xNew, bindings);
var lambda = Expression.Lambda<Func<T, S>>(xInit, xParameter);
return lambda.Compile();
}
and then to invoke it I'm using
CreateSelectStatement<SP_ProjectAnalysis_NP, dynamic>(columns)
Expected outcome should be the fields that are loading correctly but the type of the object is as if I used p => new { }. Currently I am getting object as the return type and trying to perform the following gets me 0 properties:
private static DataTable ToDataTable<T>(IList<T> list, string tableName = "table")
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
Aucun commentaire:
Enregistrer un commentaire