Queryable.Mapper()

This commit is contained in:
sunkaixuan
2018-10-14 13:10:08 +08:00
parent eb66d8ef0b
commit f5d723162c
6 changed files with 207 additions and 8 deletions

View File

@@ -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);//inViewModelStudent3[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();
}
}
}

View File

@@ -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; }
}
}