Add ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField);

This commit is contained in:
sunkaixuan
2019-05-10 20:11:32 +08:00
parent 22a262d4bb
commit d5e67042ca
5 changed files with 219 additions and 0 deletions

View File

@@ -15,6 +15,32 @@ namespace OrmTest
JoinTable();
Async();
NoEntity();
Mapper();
}
private static void Mapper()
{
Console.WriteLine("");
Console.WriteLine("#### Mapper Start ####");
var db = GetInstance();
//Creater Table
db.CodeFirst.InitTables(typeof(Tree));
db.DbMaintenance.TruncateTable("tree");
db.Insertable(new Tree() { Id = 1, Name = "root" }).ExecuteCommand();
db.Insertable(new Tree() { Id = 11, Name = "child1",ParentId=1 }).ExecuteCommand();
db.Insertable(new Tree() { Id = 12, Name = "child2",ParentId=1 }).ExecuteCommand();
db.Insertable(new Tree() { Id = 2, Name = "root" }).ExecuteCommand();
db.Insertable(new Tree() { Id = 22, Name = "child3", ParentId = 2 }).ExecuteCommand();
var list=db.Queryable<Tree>()
//parent=(select * from parent where id=it.parent)
.Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id)
//Child=(select * from parent where ParentId=it.id)
.Mapper(it => it.Child, it => it.Id, it => it.Parent.ParentId)
.ToList();
Console.WriteLine("#### End Start ####");
}
private static void NoEntity()

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class Tree
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public Tree Parent { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<Tree> Child { get; set; }
}
}

View File

@@ -56,6 +56,7 @@
<Compile Include="Demo\DemoD_DbFirst.cs" />
<Compile Include="Demo\DemoE_CodeFirst.cs" />
<Compile Include="Models\Custom.cs" />
<Compile Include="Models\Mappers.cs" />
<Compile Include="Models\MyCustomAttributeTable.cs" />
<Compile Include="Models\Order.cs" />
<Compile Include="Models\OrderItem.cs" />