You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

183 lines
6.6 KiB

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, TOut>(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 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<T> GetData<T, TKey>(DbContext db, Expression<Func<T, T>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order)
where T : class
{
var query = db.Set<T>().AsExpandable()
.Where(where)
.OrderBy(order)
.Select(select);
// Console.WriteLine(query.ToString());
var list = query.ToList();
return list;
}
public static List<dynamic> GetData<T, TKey>(DbContext db, Expression<Func<T, dynamic>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, out int count)
where T : class
{
count = db.Set<T>().AsExpandable().Where(where).Count();
var list = db.Set<T>().AsExpandable()
.Where(where)
.OrderBy(order)
.Select(select).ToList();
return list;
}
public static List<dynamic> GetPagedDataAsc<T, TKey>(DbContext db, Expression<Func<T, dynamic>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize,
out int total)
where T : class
{
total = db.Set<T>().AsExpandable().Count(where);
var list = db.Set<T>().AsExpandable()
.Where(where)
.OrderBy(order)
.Select(select)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
return list;
}
public static List<T> GetPagedDataAsc<T, TKey>(DbContext db, Expression<Func<T, T>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize,
out int total)
where T : class
{
total = db.Set<T>().AsExpandable().Count(where);
var list = db.Set<T>().AsExpandable()
.Where(where)
.OrderBy(order)
.Select(select)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
return list;
}
public static List<T> GetPagedDataAsc<T, TKey>(List<T> sourceList, Expression<Func<T, T>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize,
out int total)
where T : class
{
List<T> 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<dynamic> GetPagedDataDesc<T, TKey>(DbContext db, Expression<Func<T, dynamic>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize,
out int total)
where T : class
{
total = db.Set<T>().AsExpandable().Where(where).Count();
var list = db.Set<T>().AsExpandable()
.Where(where)
.OrderByDescending(order)
.Select(select)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
return list;
}
public static List<T> GetPagedDataDesc<T, TKey>(DbContext db, Expression<Func<T, T>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize,
out int total)
where T : class
{
total = db.Set<T>().AsExpandable().Where(where).Count();
var list = db.Set<T>().AsExpandable()
.Where(where)
.OrderByDescending(order)
.Select(select)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
return list;
}
public static List<T> GetPagedDataDesc<T, TKey>(List<T> sourceList, Expression<Func<T, T>> select,
Expression<Func<T, bool>> where, Expression<Func<T, TKey>> 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;
}
}
}