You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using FreeSql;
|
|
using FreeSql.Internal.CommonProvider;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Common.Data
|
|
{
|
|
/// <summary>
|
|
/// Freesql 拓展
|
|
/// </summary>
|
|
public static class FreesqlExtension
|
|
{/// <summary>
|
|
/// 忽略指定的列查询 只支持表
|
|
/// </summary>
|
|
/// <typeparam name="T1"></typeparam>
|
|
/// <param name="that"></param>
|
|
/// <param name="selector"></param>
|
|
/// <returns></returns>
|
|
public static List<T1> ToListIgnore<T1>(this ISelect<T1> that, Expression<Func<T1, object>> selector)
|
|
{
|
|
if (selector == null) return that.ToList();
|
|
var s0p = that as Select0Provider;
|
|
var tb = s0p._tables[0];
|
|
var parmExp = tb.Parameter ?? Expression.Parameter(tb.Table.Type, tb.Alias);
|
|
var initExps = tb.Table.Columns.Values
|
|
.Where(a => a.Attribute.IsIgnore == false)
|
|
.Select(a => new
|
|
{
|
|
exp = Expression.Bind(tb.Table.Properties[a.CsName], Expression.MakeMemberAccess(parmExp, tb.Table.Properties[a.CsName])),
|
|
ignored = TestMemberExpressionVisitor.IsExists(selector, Expression.MakeMemberAccess(parmExp, tb.Table.Properties[a.CsName]))
|
|
})
|
|
.Where(a => a.ignored == false)
|
|
.Select(a => a.exp)
|
|
.ToArray();
|
|
var lambda = Expression.Lambda<Func<T1, T1>>(
|
|
Expression.MemberInit(
|
|
Expression.New(tb.Table.Type),
|
|
initExps
|
|
),
|
|
parmExp
|
|
);
|
|
return that.ToList(lambda);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 忽略指定的列查询 只支持表
|
|
/// </summary>
|
|
/// <typeparam name="T1"></typeparam>
|
|
/// <param name="that"></param>
|
|
/// <param name="selector"></param>
|
|
/// <returns></returns>
|
|
public static async Task<List<T1>> ToListIgnoreAsync<T1>(this ISelect<T1> that, Expression<Func<T1, object>> selector)
|
|
{
|
|
if (selector == null) return that.ToList();
|
|
var s0p = that as Select0Provider;
|
|
var tb = s0p._tables[0];
|
|
var parmExp = tb.Parameter ?? Expression.Parameter(tb.Table.Type, tb.Alias);
|
|
var initExps = tb.Table.Columns.Values
|
|
.Where(a => a.Attribute.IsIgnore == false)
|
|
.Select(a => new
|
|
{
|
|
exp = Expression.Bind(tb.Table.Properties[a.CsName], Expression.MakeMemberAccess(parmExp, tb.Table.Properties[a.CsName])),
|
|
ignored = TestMemberExpressionVisitor.IsExists(selector, Expression.MakeMemberAccess(parmExp, tb.Table.Properties[a.CsName]))
|
|
})
|
|
.Where(a => a.ignored == false)
|
|
.Select(a => a.exp)
|
|
.ToArray();
|
|
var lambda = Expression.Lambda<Func<T1, T1>>(
|
|
Expression.MemberInit(
|
|
Expression.New(tb.Table.Type),
|
|
initExps
|
|
),
|
|
parmExp
|
|
);
|
|
return await that.ToListAsync(lambda);
|
|
}
|
|
|
|
}
|
|
|
|
class TestMemberExpressionVisitor : ExpressionVisitor
|
|
{
|
|
public string MemberExpString;
|
|
public bool Result { get; private set; }
|
|
|
|
public static bool IsExists(Expression selector, Expression memberExp)
|
|
{
|
|
var visitor = new TestMemberExpressionVisitor { MemberExpString = memberExp.ToString() };
|
|
visitor.Visit(selector);
|
|
return visitor.Result;
|
|
}
|
|
protected override Expression VisitMember(MemberExpression node)
|
|
{
|
|
if (!Result && node.ToString() == MemberExpString) Result = true;
|
|
return node;
|
|
}
|
|
}
|
|
}
|