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 { /// /// Freesql 拓展 /// public static class FreesqlExtension {/// /// 忽略指定的列查询 只支持表 /// /// /// /// /// public static List ToListIgnore(this ISelect that, Expression> 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>( Expression.MemberInit( Expression.New(tb.Table.Type), initExps ), parmExp ); return that.ToList(lambda); } /// /// 忽略指定的列查询 只支持表 /// /// /// /// /// public static async Task> ToListIgnoreAsync(this ISelect that, Expression> 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>( 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; } } }