mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 14:04:44 +08:00
Queryable.Mapper()
This commit is contained in:
parent
eb66d8ef0b
commit
f5d723162c
@ -6,7 +6,7 @@ using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class Mapper : DemoBase
|
||||
public class Mapper : DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
@ -17,12 +17,37 @@ namespace OrmTest.Demo
|
||||
.Select<ViewModelStudent3>().ToList();
|
||||
|
||||
|
||||
var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
|
||||
.Select<ViewModelStudent3>().Mapper(it=> {
|
||||
it.Name = it.Name==null?"默认值":it.Name;
|
||||
it.CreateTime = DateTime.Now.Date;
|
||||
it.Id = it.Id*1000;
|
||||
}).ToList();
|
||||
var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
|
||||
|
||||
.Mapper((it, cache) =>
|
||||
{
|
||||
|
||||
var allSchools = cache.GetListByPrimaryKeys<School>(i => i.SchoolId);//in(ViewModelStudent3[0].id , ViewModelStudent3[1].id...)
|
||||
|
||||
//Equal to the following writing.
|
||||
//var allSchools2= cache.Get(list =>
|
||||
// {
|
||||
// var ids=list.Select(i => it.SchoolId).ToList();
|
||||
// return db.Queryable<School>().In(ids).ToList();
|
||||
//});Complex writing metho
|
||||
|
||||
|
||||
|
||||
it.School = allSchools.FirstOrDefault(i => i.Id == it.SchoolId);//one to one
|
||||
|
||||
it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList();//one to many
|
||||
|
||||
}).ToList();
|
||||
|
||||
|
||||
var s13 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
|
||||
|
||||
.Mapper((it, cache) =>
|
||||
{
|
||||
it.Schools = db.Queryable<School>().Where(i => i.Id == it.SchoolId).ToList();
|
||||
}).ToList();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,5 +20,7 @@ namespace OrmTest.Models
|
||||
public string SchoolName { get; set; }
|
||||
public string School_Name { get; set; }
|
||||
public string ScId { get; set; }
|
||||
public School School { get; set; }
|
||||
public List<School> Schools { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ namespace SqlSugar
|
||||
public MappingTableList OldMappingTableList { get; set; }
|
||||
public MappingTableList QueryableMappingTableList { get; set; }
|
||||
public Action<T> MapperAction { get; set; }
|
||||
public Action<T, MapperCache<T>> MapperActionWithCache { get; set; }
|
||||
public bool IsCache { get; set; }
|
||||
public int CacheTime { get; set; }
|
||||
public bool IsAs { get; set; }
|
||||
@ -75,6 +76,11 @@ namespace SqlSugar
|
||||
this.MapperAction = mapperAction;
|
||||
return this;
|
||||
}
|
||||
public virtual ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction)
|
||||
{
|
||||
this.MapperActionWithCache = mapperAction;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<T> AddParameters(object parameters)
|
||||
{
|
||||
@ -185,6 +191,14 @@ namespace SqlSugar
|
||||
Where(SqlBuilder.SqlFalse);
|
||||
return this;
|
||||
}
|
||||
if (pkValues.Length == 1&& pkValues.First() is IEnumerable) {
|
||||
var newValues =new List<object>();
|
||||
foreach (var item in pkValues.First() as IEnumerable)
|
||||
{
|
||||
newValues.Add(item);
|
||||
}
|
||||
return In(newValues);
|
||||
}
|
||||
var pks = GetPrimaryKeys().Select(it => SqlBuilder.GetTranslationTableName(it)).ToList();
|
||||
Check.Exception(pks == null || pks.Count != 1, "Queryable.In(params object[] pkValues): Only one primary key");
|
||||
string filed = pks.FirstOrDefault();
|
||||
@ -1114,13 +1128,28 @@ namespace SqlSugar
|
||||
{
|
||||
if (typeof(TResult) == typeof(T))
|
||||
{
|
||||
MapperAction((T)Convert.ChangeType(item, typeof(T)));
|
||||
this.MapperAction((T)Convert.ChangeType(item, typeof(T)));
|
||||
}
|
||||
else {
|
||||
Check.Exception(true, "{0} and {1} are not a type, Try .select().mapper().ToList", typeof(TResult).FullName,typeof(T).FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.MapperActionWithCache != null)
|
||||
{
|
||||
if (typeof(TResult) == typeof(T))
|
||||
{
|
||||
var list = (List<T>)Convert.ChangeType(result, typeof(List<T>));
|
||||
var mapperCache = new MapperCache<T>(list,this.Context);
|
||||
foreach (T item in list)
|
||||
{
|
||||
this.MapperActionWithCache(item, mapperCache);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Check.Exception(true, "{0} and {1} are not a type, Try .select().mapper().ToList", typeof(TResult).FullName, typeof(T).FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected int GetCount()
|
||||
|
141
Src/Asp.Net/SqlSugar/Entities/MapperCache.cs
Normal file
141
Src/Asp.Net/SqlSugar/Entities/MapperCache.cs
Normal file
@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MapperCache<T>
|
||||
{
|
||||
private Dictionary<string, object> caches = new Dictionary<string, object>();
|
||||
private List<T> _list { get; set; }
|
||||
private SqlSugarClient _context { get; set; }
|
||||
private MapperCache()
|
||||
{
|
||||
}
|
||||
public MapperCache(List<T> list, SqlSugarClient context)
|
||||
{
|
||||
_list = list;
|
||||
_context = context;
|
||||
}
|
||||
public Result Get<Result>(Func<List<T>, Result> action)
|
||||
{
|
||||
string key = "Get" + typeof(Result) + action.GetHashCode().ToString();
|
||||
if (caches.ContainsKey(key))
|
||||
{
|
||||
return (Result)caches[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = action(_list);
|
||||
caches.Add(key, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, double?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, double?>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, double> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, double>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, decimal?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, decimal?>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, decimal> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, decimal>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, int?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result,int?>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, int> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, int>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, long?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, long?>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, long> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, long>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, string> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, string>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, Guid> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, Guid>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, Guid?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, Guid?>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, DateTime> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, DateTime>(action, key);
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, DateTime?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
|
||||
return GetListByPrimaryKeys<Result, DateTime?>(action, key);
|
||||
}
|
||||
}
|
||||
private List<Result> GetListByPrimaryKeys<Result,FieldType>(Func<T, FieldType> action, string key) where Result : class, new()
|
||||
{
|
||||
if (caches.ContainsKey(key))
|
||||
{
|
||||
return (List<Result>)caches[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
var ids = _list.Select(action).ToList().Distinct().ToList();
|
||||
var result = _context.Queryable<Result>().In(ids).ToList();
|
||||
caches.Add(key, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ namespace SqlSugar
|
||||
ISugarQueryable<T> With(string withString);
|
||||
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
||||
ISugarQueryable<T> Mapper(Action<T> mapperAction);
|
||||
ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction);
|
||||
ISugarQueryable<T> AddParameters(object parameters);
|
||||
ISugarQueryable<T> AddParameters(SugarParameter[] parameters);
|
||||
ISugarQueryable<T> AddParameters(List<SugarParameter> parameters);
|
||||
|
@ -78,6 +78,7 @@
|
||||
<Compile Include="Entities\CacheKey.cs" />
|
||||
<Compile Include="Entities\ConditionalModel.cs" />
|
||||
<Compile Include="Entities\ConnMoreSettings.cs" />
|
||||
<Compile Include="Entities\MapperCache.cs" />
|
||||
<Compile Include="Entities\SlaveConnectionConfig.cs" />
|
||||
<Compile Include="Enum\ConditionalType.cs" />
|
||||
<Compile Include="Enum\DbType.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user