diff --git a/Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs b/Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs index b0f6ad9cf..fe4bf41f3 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs @@ -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().ToList(); - var s12 = db.Queryable((st, sc) => st.SchoolId == sc.Id) - .Select().Mapper(it=> { - it.Name = it.Name==null?"默认值":it.Name; - it.CreateTime = DateTime.Now.Date; - it.Id = it.Id*1000; - }).ToList(); + var s12 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select() + + .Mapper((it, cache) => + { + + var allSchools = cache.GetListByPrimaryKeys(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().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((st, sc) => st.SchoolId == sc.Id).Select() + + .Mapper((it, cache) => + { + it.Schools = db.Queryable().Where(i => i.Id == it.SchoolId).ToList(); + }).ToList(); + + } } } diff --git a/Src/Asp.Net/SqlServerTest/Models/ViewModelStudent.cs b/Src/Asp.Net/SqlServerTest/Models/ViewModelStudent.cs index a97dfd98c..75332c12f 100644 --- a/Src/Asp.Net/SqlServerTest/Models/ViewModelStudent.cs +++ b/Src/Asp.Net/SqlServerTest/Models/ViewModelStudent.cs @@ -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 Schools { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index ac832c983..23379cebc 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -23,6 +23,7 @@ namespace SqlSugar public MappingTableList OldMappingTableList { get; set; } public MappingTableList QueryableMappingTableList { get; set; } public Action MapperAction { get; set; } + public Action> 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 Mapper(Action> mapperAction) + { + this.MapperActionWithCache = mapperAction; + return this; + } public virtual ISugarQueryable 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(); + 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)Convert.ChangeType(result, typeof(List)); + var mapperCache = new MapperCache(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() diff --git a/Src/Asp.Net/SqlSugar/Entities/MapperCache.cs b/Src/Asp.Net/SqlSugar/Entities/MapperCache.cs new file mode 100644 index 000000000..083a39e8b --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Entities/MapperCache.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public class MapperCache + { + private Dictionary caches = new Dictionary(); + private List _list { get; set; } + private SqlSugarClient _context { get; set; } + private MapperCache() + { + } + public MapperCache(List list, SqlSugarClient context) + { + _list = list; + _context = context; + } + public Result Get(Func, 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 GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + public List GetListByPrimaryKeys(Func action) where Result : class, new() + { + { + string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); + return GetListByPrimaryKeys(action, key); + } + } + private List GetListByPrimaryKeys(Func action, string key) where Result : class, new() + { + if (caches.ContainsKey(key)) + { + return (List)caches[key]; + } + else + { + var ids = _list.Select(action).ToList().Distinct().ToList(); + var result = _context.Queryable().In(ids).ToList(); + caches.Add(key, result); + return result; + } + } + } +} diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index d98d62b4b..bab64f28b 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -20,6 +20,7 @@ namespace SqlSugar ISugarQueryable With(string withString); ISugarQueryable Filter(string FilterName, bool isDisabledGobalFilter = false); ISugarQueryable Mapper(Action mapperAction); + ISugarQueryable Mapper(Action> mapperAction); ISugarQueryable AddParameters(object parameters); ISugarQueryable AddParameters(SugarParameter[] parameters); ISugarQueryable AddParameters(List parameters); diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index ee6dee0d7..2fc62c5f4 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -78,6 +78,7 @@ +