mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-26 10:49:20 +08:00
Queryable.Mapper()
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user