下面是代码,其实主要就是用Expression生成指定的lambda,
算是抛砖引玉吧
/// <summary>
/// 多条件查询
/// </summary>
/// <typeparam name="TEntity">映射实体</typeparam>
public class IQueryableForSearch<TEntity> where TEntity : TEntityFather
{
/// <summary>
/// 通过反射实体和查询模型动态拼接查询条件
/// </summary>
/// <typeparam name="T">条件模型</typeparam>
public static IQueryable<TEntity> GetIQueryable<T>(DbSet<TEntity> tEntity, T t)
{
IQueryable<TEntity> q1 = tEntity;
if (t == null) return q1;
var tPros = typeof(T).GetProperties();
var tEntityPros = typeof(TEntity).GetProperties();
foreach (var tPro in tPros)
{
foreach (var tEntityPro in tEntityPros)
{
var tProVal = tPro.GetValue(t);
var tProType = tPro.PropertyType;
if (tPro.Name.Equals(tEntityPro.Name))
{
if ((tProType == typeof(string) && !string.IsNullOrEmpty((string)tProVal))
|| (tProType.IsEnum && (byte)tProVal != 0))
q1 = q1.Where(PropertyEquals(tEntityPro, tProVal));
if (tEntityPro.PropertyType == typeof(DateTime?) && tProType == typeof(Search_DateTime))
{
var sdt = (Search_DateTime)tProVal;
if (sdt != null)
{
if (sdt.start != null)
q1 = q1.Where(PropertyGreaterThanOrEqual<object>(tEntityPro, sdt.start));
if (sdt.end != null)
q1 = q1.Where(PropertyLessThanOrEqual<object>(tEntityPro, sdt.end));
}
}
}
}
}
//System.Diagnostics.Trace.WriteLine(q1.ToString());
return q1;
}
/// <summary>
/// 获取是否相等的lambda
/// </summary>
/// <typeparam name="TValue"></typeparam>
public static Expression<Func<TEntity, bool>> PropertyEquals<TValue>(PropertyInfo property, TValue value)
{
var param = Expression.Parameter(typeof(TEntity));
var body = Expression.Equal(Expression.Property(param, property), Expression.Constant(value));
return Expression.Lambda<Func<TEntity, bool>>(body, param);
}
/// <summary>
/// 时间点大于等于
/// </summary>
public static Expression<Func<TEntity, bool>> PropertyGreaterThanOrEqual<TValue>(PropertyInfo property, TValue value)
{
var param = Expression.Parameter(typeof(TEntity));
var body = Expression.GreaterThanOrEqual(Expression.Property(param, property), Expression.Constant(value, typeof(DateTime?)));
return Expression.Lambda<Func<TEntity, bool>>(body, param);
}
/// <summary>
/// 时间点小于等于
/// </summary>
public static Expression<Func<TEntity, bool>> PropertyLessThanOrEqual<TValue>(PropertyInfo property, TValue value)
{
var param = Expression.Parameter(typeof(TEntity));
var body = Expression.LessThanOrEqual(Expression.Property(param, property), Expression.Constant(value, typeof(DateTime?)));
return Expression.Lambda<Func<TEntity, bool>>(body, param);
}
/// <summary>
/// 多条件查询
/// </summary>
/// <param name="tEntity">映射实体</param>
/// <param name="objs">查询条件列表</param>
/// <returns></returns>
public static IQueryable<TEntity> GetIQueryable(DbSet<TEntity> tEntity, params object[] objs)
{
IQueryable<TEntity> q1 = tEntity;
var tEntityPros = typeof(TEntity).GetProperties();
foreach (var obj in objs)
{
foreach (var tEntityPro in tEntityPros)
{
var objType = obj.GetType();
if (objType.Name.Equals(tEntityPro.Name))
{
if ((objType == typeof(string) && !string.IsNullOrEmpty((string)obj))
|| (objType.IsEnum && (byte)obj != 0))
q1 = q1.Where(PropertyEquals(tEntityPro, obj));
if (tEntityPro.PropertyType == typeof(DateTime?) && objType == typeof(Search_DateTime))
{
var sdt = (Search_DateTime)obj;
if (sdt != null)
{
if (sdt.start != null)
q1 = q1.Where(PropertyGreaterThanOrEqual<object>(tEntityPro, sdt.start));
if (sdt.end != null)
q1 = q1.Where(PropertyLessThanOrEqual<object>(tEntityPro, sdt.end));
}
}
}
}
}
//System.Diagnostics.Trace.WriteLine(q1.ToString());
return q1;
}
}