Add Queryable<T>().Mapper

This commit is contained in:
sunkaixuan 2018-10-14 09:26:53 +08:00
parent a495651828
commit eb66d8ef0b
5 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,28 @@
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class Mapper : DemoBase
{
public static void Init()
{
var db = GetInstance();
//auto fill ViewModelStudent3
var s11 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
.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();
}
}
}

View File

@ -51,6 +51,7 @@ namespace OrmTest
Demo.ExtSqlFun.Init();
Demo.QueryableView.Init();
Demo.AttributeDemo.Init();
Demo.Mapper.Init();
}
}
}

View File

@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Demos\F_VersionValidation.cs" />
<Compile Include="Demos\G_Mapper.cs" />
<Compile Include="Models\CapitalEntity.cs" />
<Compile Include="Config.cs" />
<Compile Include="Demos\5_CodeFirst.cs" />

View File

@ -22,6 +22,7 @@ namespace SqlSugar
public ISqlBuilder SqlBuilder { get; set; }
public MappingTableList OldMappingTableList { get; set; }
public MappingTableList QueryableMappingTableList { get; set; }
public Action<T> MapperAction { get; set; }
public bool IsCache { get; set; }
public int CacheTime { get; set; }
public bool IsAs { get; set; }
@ -70,6 +71,11 @@ namespace SqlSugar
return this;
}
public virtual ISugarQueryable<T> Mapper(Action<T> mapperAction) {
this.MapperAction = mapperAction;
return this;
}
public virtual ISugarQueryable<T> AddParameters(object parameters)
{
if (parameters != null)
@ -1096,8 +1102,27 @@ namespace SqlSugar
result = GetData<TResult>(sqlObj);
}
RestoreMapping();
_Mapper(result);
return result;
}
protected void _Mapper<TResult>(List<TResult> result)
{
if (this.MapperAction != null)
{
foreach (TResult item in result)
{
if (typeof(TResult) == typeof(T))
{
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);
}
}
}
}
protected int GetCount()
{
var sql = string.Empty;
@ -1233,6 +1258,7 @@ namespace SqlSugar
asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue;
asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter;
asyncQueryableBuilder.PartitionByValue = this.QueryBuilder.PartitionByValue;
asyncQueryableBuilder.JoinExpression = this.QueryBuilder.JoinExpression;
return asyncQueryable;
}
#endregion

View File

@ -19,6 +19,7 @@ namespace SqlSugar
ISugarQueryable<T> AS(string tableName);
ISugarQueryable<T> With(string withString);
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
ISugarQueryable<T> Mapper(Action<T> mapperAction);
ISugarQueryable<T> AddParameters(object parameters);
ISugarQueryable<T> AddParameters(SugarParameter[] parameters);
ISugarQueryable<T> AddParameters(List<SugarParameter> parameters);