using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using LinqKit; namespace ChangkeTec.Utils { public static class EntitiesHelper { public static TOut Trans(TIn tFrom) where TOut : new() { var tTo = new TOut(); var propsOfT1 = tTo.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var propsOfT2 = tFrom.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var p1 in propsOfT1) { foreach (var p2 in propsOfT2) { if (p2.Name != p1.Name) continue; var value = p2.GetValue(tFrom); p1.SetValue(tTo, value, null); break; } } return tTo; } public static string GetPropertiesString(T t, bool withName = true) { var peroperties = t.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var sb = new StringBuilder(); foreach (var property in peroperties) { var value = property.GetValue(t) ?? "~null"; if (property.PropertyType.IsGenericType || property.PropertyType.IsArray) { // var list = (IList)value; // sb.Append($"{property.Name}:{list.Count},"); } else { if (withName) sb.Append($"{property.Name}:"); sb.Append($"{value},"); } } return sb.ToString(); } public static List GetData(DbContext db, Expression> select, Expression> where, Expression> order) where T : class { var query = db.Set().AsExpandable() .Where(where) .OrderBy(order) .Select(select); // Console.WriteLine(query.ToString()); var list = query.ToList(); return list; } public static List GetData(DbContext db, Expression> select, Expression> where, Expression> order, out int count) where T : class { count = db.Set().AsExpandable().Where(where).Count(); var list = db.Set().AsExpandable() .Where(where) .OrderBy(order) .Select(select).ToList(); return list; } public static List GetPagedDataAsc(DbContext db, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { total = db.Set().AsExpandable().Count(where); var list = db.Set().AsExpandable() .Where(where) .OrderBy(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); return list; } public static List GetPagedDataAsc(DbContext db, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { total = db.Set().AsExpandable().Count(where); var list = db.Set().AsExpandable() .Where(where) .OrderBy(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); return list; } public static List GetPagedDataAsc(List sourceList, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { List list = null; total = 0; try { total = sourceList.Count(where.Compile()); list = sourceList.Where(where.Compile()).AsQueryable() .OrderBy(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); } catch (Exception e) { Console.WriteLine(e); } return list; } public static List GetPagedDataDesc(DbContext db, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { total = db.Set().AsExpandable().Where(where).Count(); var list = db.Set().AsExpandable() .Where(where) .OrderByDescending(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); return list; } public static List GetPagedDataDesc(DbContext db, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { total = db.Set().AsExpandable().Where(where).Count(); var list = db.Set().AsExpandable() .Where(where) .OrderByDescending(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); return list; } public static List GetPagedDataDesc(List sourceList, Expression> select, Expression> where, Expression> order, int pageIndex, int pageSize, out int total) where T : class { total = sourceList.Count(where.Compile()); var list = sourceList .Where(where.Compile()).AsQueryable() .OrderByDescending(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); return list; } } }