通过此ef将在数据库中执行多少sql查询

iyr7buue  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(320)

我有以下内容,想知道有多少查询将在数据库中通过这个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(); }
}

}

tcomlyy6

tcomlyy61#

在解决编译器错误后,它将是一个查询:

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(),
    });

相关问题