#region 私有函数
//Expression表达式 包含字符串
private static Expression<Func<T, bool>> GetConditionExpression<T>(string[] options, string fieldName)
{
ParameterExpression left = Expression.Parameter(typeof(T), "c");//c=>
Expression expression = Expression.Constant(false);
foreach (var optionName in options)
{
Expression right = Expression.Call
(
Expression.Property(left, typeof(T).GetProperty(fieldName)), //c.DataSourceName
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),// 反射使用.Contains()方法
Expression.Constant(optionName) // .Contains(optionName)
);
expression = Expression.Or(right, expression);//c.DataSourceName.contain("") || c.DataSourceName.contain("")
}
Expression<Func<T, bool>> finalExpression
= Expression.Lambda<Func<T, bool>>(expression, new ParameterExpression[] { left });
return finalExpression;
}
//Expression表达式 selector (rec) => new ParadigmSearchListData { Number = rec.NUMBER, Name = rec.RES_NAME };
//参数为 源数据(TSource)的字段
private static Expression<Func<TSource, TResult>> CreateSelecter<TSource, TResult>(string colNumber, string colName)
{
Expression<Func<TSource, TResult>> selector = null;
//(rec)
ParameterExpression param = Expression.Parameter(typeof(TSource), "rec");
//new ParadigmSearchListData
var v0 = Expression.New(typeof(TResult));
//Number
var v1 = typeof(TResult).GetProperty("Number");
//rec.NUMBER
var v2 = Expression.Property(param, typeof(TSource).GetProperty(colNumber));
//Name
var v3 = typeof(TResult).GetProperty("Name");
//rec.RES_NAME
var v4 = Expression.Property(param, typeof(TSource).GetProperty(colName));
Expression body = Expression.MemberInit(v0,
//{ Number = rec.NUMBER, Name = rec.RES_NAME }
new MemberBinding[]
{
//Number = rec.NUMBER
Expression.Bind(v1, v2),
//Name = rec.RES_NAME
Expression.Bind(v3, v4)
});
selector = (Expression<Func<TSource, TResult>>)Expression.Lambda(body, param);
return selector;
}
//select 参数colName为 源数据(TSource)的字段
private static IQueryable<TResult> GetListDataSelect<TSouce, TResult>(IQueryable<TSouce> entTable, params string[] colName)
{
//依据IQueryable数据源构造一个查询
IQueryable<TSouce> custs = entTable;
//组建一个表达式树来创建一个参数
Expression selector = null;
if (colName.Count() == 2)
{
selector = CreateSelecter<TSouce, TResult>(colName[0], colName[1]);
}
//组建表达式树:Select(c=>c.ContactName)
Expression expr = Expression.Call(typeof(Queryable), "Select",
new Type[] { typeof(TSouce), typeof(TResult) },
Expression.Constant(custs), selector);
//使用表达式树来生成动态查询
IQueryable<TResult> query = entTable.AsQueryable()
.Provider.CreateQuery<TResult>(expr);
return query;
}
//where
private static IQueryable<TSouce> GetListDataWhere<TSouce, TResult>(
IQueryable<TSouce> entTable, string[] options, string fieldName)
{
//包含
Expression whereor = GetConditionExpression<TSouce>(options, fieldName);
Expression expr = Expression.Call(typeof(Queryable), "Where",
new Type[] { typeof(TSouce) },
Expression.Constant(entTable), whereor);
//生成动态查询
IQueryable<TSouce> query = entTable.AsQueryable()
.Provider.CreateQuery<TSouce>(expr);
return query;
}
//获得数据 name
static private IQueryable<TResult> GetListDataName<TSouce, TResult>(IQueryable<TSouce> entTable,
string wordKey, string whereColName, params string[] selectColName)
{
return GetListDataSelect<TSouce, TResult>(
GetListDataWhere<TSouce, TResult>(entTable, new string[] { wordKey }, whereColName),
selectColName
);
}
#endregion
//获得数据 材料与器械 Name
static public List<ParadigmSearchListData> GetListDataMaterialApparatusName(string wordKey)
{
List<ParadigmSearchListData> listData = null;
using (ParadigmSearch.ParadigmSearchEEntities ent = new ParadigmSearch.ParadigmSearchEEntities())
{
listData = GetListDataName<ParadigmSearch.T_MATERIAL_APPARATUS, ParadigmSearchListData>(
ent.T_MATERIAL_APPARATUS, wordKey, "RES_NAME", "NUMBER", "RES_NAME").ToList();
}
return listData;
}