隐藏

efcore linq执行多表联接查询分组

发布:2023/12/7 15:23:45作者:大数据 来源:大数据 浏览次数:518

//linq执行多表查询
var q = from a in dbContext.Sxcountylevelcityinfo
join b in dbContext.Sxprefecturelevelcityinfo
on a.PrefectureId equals b.PrefectureId
join c in table_c on c.id equals b.id
select new { a, b };

 

//efcore执行多表查询
var p = dbContext.Student.Join(dbContext.School, stu=> stu.SchoolId,sch => sch.Id, (stu, sch) => new {stu,sch.SchoolName });

https://www.cnblogs.com/CoderAyu/p/8576822.html

 

//通过include显示加载相关联接表,前提是在Dbcontext中的模型类中建立关系,如一对多等

var xx= dbContext.Sxcountylevelcityinfo.Include(o=>o.Sxprefecturelevelcityinfo);

 

//efcore执行sql语句,如增,删,改以及创建表等,这里一般不执行查询
var s = dbContext.Database.ExecuteSqlCommand("update sxprefecturelevelcityinfo set comments='b' where id=13");

var student = await _context.Students .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id);//执行异步查询

groupby sum组合查询统计求和某列:

var x = dbContext.User.GroupBy(o => o.CategoryName).Select(x => new { x.Key, Total = x.Sum(o => o.PersonNumber) });

x.Key代表Key=x.Key的简写,将来按分组名取数据。当然也可以写成Name=x.key,那么取数据就用Name

 

GroupBy根据多个字段分组使用方式:

 

 

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!