mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add Queryable<T>().Mapper
This commit is contained in:
parent
a495651828
commit
eb66d8ef0b
28
Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs
Normal file
28
Src/Asp.Net/SqlServerTest/Demos/G_Mapper.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -51,6 +51,7 @@ namespace OrmTest
|
|||||||
Demo.ExtSqlFun.Init();
|
Demo.ExtSqlFun.Init();
|
||||||
Demo.QueryableView.Init();
|
Demo.QueryableView.Init();
|
||||||
Demo.AttributeDemo.Init();
|
Demo.AttributeDemo.Init();
|
||||||
|
Demo.Mapper.Init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Demos\F_VersionValidation.cs" />
|
<Compile Include="Demos\F_VersionValidation.cs" />
|
||||||
|
<Compile Include="Demos\G_Mapper.cs" />
|
||||||
<Compile Include="Models\CapitalEntity.cs" />
|
<Compile Include="Models\CapitalEntity.cs" />
|
||||||
<Compile Include="Config.cs" />
|
<Compile Include="Config.cs" />
|
||||||
<Compile Include="Demos\5_CodeFirst.cs" />
|
<Compile Include="Demos\5_CodeFirst.cs" />
|
||||||
|
@ -22,6 +22,7 @@ namespace SqlSugar
|
|||||||
public ISqlBuilder SqlBuilder { get; set; }
|
public ISqlBuilder SqlBuilder { get; set; }
|
||||||
public MappingTableList OldMappingTableList { get; set; }
|
public MappingTableList OldMappingTableList { get; set; }
|
||||||
public MappingTableList QueryableMappingTableList { get; set; }
|
public MappingTableList QueryableMappingTableList { get; set; }
|
||||||
|
public Action<T> MapperAction { get; set; }
|
||||||
public bool IsCache { get; set; }
|
public bool IsCache { get; set; }
|
||||||
public int CacheTime { get; set; }
|
public int CacheTime { get; set; }
|
||||||
public bool IsAs { get; set; }
|
public bool IsAs { get; set; }
|
||||||
@ -70,6 +71,11 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual ISugarQueryable<T> Mapper(Action<T> mapperAction) {
|
||||||
|
this.MapperAction = mapperAction;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public virtual ISugarQueryable<T> AddParameters(object parameters)
|
public virtual ISugarQueryable<T> AddParameters(object parameters)
|
||||||
{
|
{
|
||||||
if (parameters != null)
|
if (parameters != null)
|
||||||
@ -1096,8 +1102,27 @@ namespace SqlSugar
|
|||||||
result = GetData<TResult>(sqlObj);
|
result = GetData<TResult>(sqlObj);
|
||||||
}
|
}
|
||||||
RestoreMapping();
|
RestoreMapping();
|
||||||
|
_Mapper(result);
|
||||||
return 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()
|
protected int GetCount()
|
||||||
{
|
{
|
||||||
var sql = string.Empty;
|
var sql = string.Empty;
|
||||||
@ -1233,6 +1258,7 @@ namespace SqlSugar
|
|||||||
asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue;
|
asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue;
|
||||||
asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter;
|
asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter;
|
||||||
asyncQueryableBuilder.PartitionByValue = this.QueryBuilder.PartitionByValue;
|
asyncQueryableBuilder.PartitionByValue = this.QueryBuilder.PartitionByValue;
|
||||||
|
asyncQueryableBuilder.JoinExpression = this.QueryBuilder.JoinExpression;
|
||||||
return asyncQueryable;
|
return asyncQueryable;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -19,6 +19,7 @@ namespace SqlSugar
|
|||||||
ISugarQueryable<T> AS(string tableName);
|
ISugarQueryable<T> AS(string tableName);
|
||||||
ISugarQueryable<T> With(string withString);
|
ISugarQueryable<T> With(string withString);
|
||||||
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
||||||
|
ISugarQueryable<T> Mapper(Action<T> mapperAction);
|
||||||
ISugarQueryable<T> AddParameters(object parameters);
|
ISugarQueryable<T> AddParameters(object parameters);
|
||||||
ISugarQueryable<T> AddParameters(SugarParameter[] parameters);
|
ISugarQueryable<T> AddParameters(SugarParameter[] parameters);
|
||||||
ISugarQueryable<T> AddParameters(List<SugarParameter> parameters);
|
ISugarQueryable<T> AddParameters(List<SugarParameter> parameters);
|
||||||
|
Loading…
Reference in New Issue
Block a user