反射枚举 desc 建拉姆达查询 sum
/// <summary>
/// 创建lambda表达式:p=>p.propertyName
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyName"></param>
/// <returns></returns>
public static Expression<Func<T, int>> CreatePropertyName<T>(string propertyName)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
return Expression.Lambda<Func<T, int>>( member , parameter);
}
定义
public enum sexID
{
[Description("Id")]
Id_1 ,
[Description("Id2")]
Id_2,
[Description("Id3")]
Id_3
}
[Index("Idu001", "Idu", true)]
public class Item
{
[Column(IsIdentity = true, IsPrimary = true)]
[DisplayName("序号")]
public int Id { get; set; }
public int Id2 { get; set; }
public int Id3 { get; set; }
[DisplayName("名称")]
public string Text { get; set; }
[DisplayName("描述")]
public string Description { get; set; }
[Column(IsPrimary = true)]
[DisplayName("序号U")]
public Guid Idu { get; set; }
}
代码
Expression<Func<Item, int>> sum = null;
sum = LambadaExpression.CreatePropertyName<Item>("Id");
var sums=fsql.Select<Item>().Sum(sum);//SELECT sum(a."Id") as1 FROM "Item" a
sum = LambadaExpression.CreatePropertyName<Item>(sexID.Id_2.ToDescriptionOrString());
var sums2=fsql.Select<Item>().Sum(sum);//SELECT sum(a."Id2") as1 FROM "Item" a
sum = LambadaExpression.CreatePropertyName<Item>(sexID.Id_3.ToDescriptionOrString());
var sums3=fsql.Select<Item>().Sum(sum);//SELECT sum(a."Id3") as1 FROM "Item" a
标签:set,get,Freesql,Sum,public,ORM,sum,Id
From: https://www.cnblogs.com/densen2014/p/16934641.html