非映射字段
gzymomo opened this issue · 3 comments
是否可以支持处理 不需要映射的属性 或者说忽略的属性
是什么原因导致有这个需求?方法1,把字段去了就可以了。其次可以找找原生dapper看看是否支撑忽略的标签
select忽略属性扩展(基于微软自带的特性):
源代码:
扩展:public static string ResolveSelect(PropertyInfo[] propertyInfos, LambdaExpression selector, int? topNum)
{
var selectFormat = topNum.HasValue ? " SELECT {1} {0} " : " SELECT {0} ";
var selectSql = "";
if (selector == null)
{
var propertyBuilder = new StringBuilder();
foreach (var propertyInfo in propertyInfos)
{
NotMappedAttribute notMappedAttribute = propertyInfo.GetCustomAttribute(typeof(NotMappedAttribute)) as NotMappedAttribute;
if (notMappedAttribute != null)
{
continue;
}
if (propertyBuilder.Length > 0)
propertyBuilder.Append(",");
propertyBuilder.AppendFormat($"{propertyInfo.GetColumnAttributeName()} {propertyInfo.Name}");
}
selectSql = string.Format(selectFormat, propertyBuilder, $" TOP {topNum} ");
}
else
{
var nodeType = selector.Body.NodeType;
if (nodeType == ExpressionType.MemberAccess)
{
var memberExpression = (MemberExpression)selector.Body;
selectSql = string.Format(selectFormat, memberExpression.Member.GetColumnAttributeName(), $" TOP {topNum} ");
}
else if (nodeType == ExpressionType.MemberInit)
{
var memberInitExpression = (MemberInitExpression)selector.Body;
selectSql = string.Format(selectFormat, string.Join(",", memberInitExpression.Bindings.Select(a => a.Member.GetColumnAttributeName())), $" TOP {topNum} ");
}
}
return selectSql;
}
ResolveOrderBy优化(支持多字段排序):
源代码:
扩展:
public static string ResolveOrderBy(Dictionary<EOrderBy, LambdaExpression> orderbyExpressionDic)
{
var orderByList = orderbyExpressionDic.Select(a =>
{
string propertyName = null;
if (a.Value.Body is MemberExpression)
{
propertyName = ((MemberExpression)a.Value.Body).Member.Name;
}
else if (a.Value.Body is UnaryExpression)
{
propertyName = ((MemberExpression)((UnaryExpression)a.Value.Body).Operand).Member.Name;
}
else if (a.Value.Body is ParameterExpression)
{
propertyName = ((ParameterExpression)a.Value.Body).Type.Name;
}
else if (a.Value.Body is NewExpression)
{
propertyName = string.Join(",", ((NewExpression)a.Value.Body).Members.Select(x => x.Name).ToArray());
}
propertyName += a.Key == EOrderBy.Desc ? " DESC" : " ASC ";
return propertyName;
}).ToList();
if (!orderByList.Any())
return "";
return "ORDER BY " + string.Join(",", orderByList);
}