[英]How many SQL Queries will be executed in the database through this EF
我有以下内容,想知道通过这个 EF 在数据库中执行多少查询?
private static dbContext dbc = new ProfileDBC();
private static IQueryable<Profile> GetProfile()
{
return dbc.Profiles;
}
private static IQueryable<Purchase> GetPurchases()
{
return from a in dbc.Amount
where a.IsPurchased
select a;
}
static void Main (string[] args)
{
var result = (from p in GetProfile()
join pa in GetPurchases()
on p.ID equals pa.ID
group p.total by pa into r
select new { r.name, totalpurchase = r.Sum( p=> p)});.ToList(); }
}
}
在您解决编译器错误之后,这将是一个查询:
Sum( p=> p)});.ToList();
如果您想获取带有子项的项目,例如带有学生的学校、带有订单的客户或带有购买的个人资料,请考虑使用 GroupJoin 而不是Join
后跟GroupBy
。 好消息是您还将获得没有购买的配置文件:
var profilesWithTheirPurchases = dbContext.Profiles.GroupJoin(dbContext.Purchases,
// parameter KeySelector: compare primary and foreign key:
profile => profile.Id, // from every Profile take the Id
purchase => purchase.ProfileId, // from every Purchase take the ProfileId
// parameter resultSelector: take every Profile with its zero or more Purchases
// to make one new
(profile, purchasesOfThisProfile) => new
{
Name = profile.Name,
TotalPurchases = purchasesOfThisId.Select(purchase => purchase.Amount)
.Sum(),
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.