63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq.Expressions;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
namespace APT.Infrastructure.EF.Extensions
|
|||
|
|
{
|
|||
|
|
public class IQueryableSelectorExtension
|
|||
|
|
{
|
|||
|
|
public static Expression<Func<TSource, TResult>> CreateSelecter<TSource, TResult>(List<string> columns)
|
|||
|
|
{
|
|||
|
|
Expression<Func<TSource, TResult>> selector = null;
|
|||
|
|
|
|||
|
|
//(rec)
|
|||
|
|
ParameterExpression param = Expression.Parameter(typeof(TSource), "rec");
|
|||
|
|
//new ParadigmSearchListData
|
|||
|
|
var v0 = Expression.New(typeof(TResult));
|
|||
|
|
|
|||
|
|
var memberBindings = new List<MemberBinding>();
|
|||
|
|
foreach (var column in columns)
|
|||
|
|
{
|
|||
|
|
var left = typeof(TResult).GetProperty(column);
|
|||
|
|
|
|||
|
|
var right = Expression.Property(param, typeof(TSource).GetProperty(column));
|
|||
|
|
|
|||
|
|
memberBindings.Add(Expression.Bind(left, right));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Expression body = Expression.MemberInit(v0,
|
|||
|
|
//{ Number = rec.NUMBER, Name = rec.RES_NAME }
|
|||
|
|
memberBindings.ToArray());
|
|||
|
|
|
|||
|
|
selector = (Expression<Func<TSource, TResult>>)Expression.Lambda(body, param);
|
|||
|
|
|
|||
|
|
return selector;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static MemberAssignment GetPropertyLambdaExpression(ParameterExpression param, string field)
|
|||
|
|
{
|
|||
|
|
string[] propertyNames = field.Split('.');
|
|||
|
|
Expression propertyAccess = param;
|
|||
|
|
Type type = param.Type;
|
|||
|
|
PropertyInfo property;
|
|||
|
|
foreach (string propertyName in propertyNames)
|
|||
|
|
{
|
|||
|
|
property = type.GetProperty(propertyName);
|
|||
|
|
if (property != null)
|
|||
|
|
{
|
|||
|
|
type = property.PropertyType;
|
|||
|
|
propertyAccess = Expression.MakeMemberAccess(propertyAccess, property);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//var left = typeof(TResult).GetProperty(column);
|
|||
|
|
//return Expression.Bind(property, Expression.Lambda(propertyAccess, param));
|
|||
|
|
//return Expression.Lambda(propertyAccess, param);
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|