简体   繁体   English

如何对 dotnet core Entity Framework 中的许多表中的数据进行分组

[英]How to GroupBy data from many tables in dotnet core Entity Framework

I'm building some marketplace web app, let's say something like e-bay.我正在构建一些市场 web 应用程序,比如说 e-bay。 Typical scenario is:典型的场景是:

User makes offer which consists of one or more items and those items are of certain type.After that other users are bidding on that offer.用户提出由一个或多个项目组成的报价,并且这些项目属于某种类型。之后,其他用户对该报价进行出价。

Here is simplified diagram.这是简化图。

图表

On SQL Fiddle ( here ) you can see both CREATE TABLE and INSERT INTO statements在 SQL Fiddle (这里) 你可以看到 CREATE TABLE 和 INSERT INTO 语句

Sample data: There are two offers.示例数据:有两个报价。 On one offer (Id 1) which consists of one item which is type of "watch". On one offer (Id 1),其中包含一件“手表”类型的商品。 There is another offer, (Id 2), which has one item which is of type "headphone".还有另一个报价 (Id 2),其中有一个“耳机”类型的项目。 On both offers there are bids.两种报价都有出价。 On watch, there are two bis;值班时,有两个二; one bid with 100 dollars and another with 120. On headphones, there are bids with 50 and 80 dollars.一个出价 100 美元,另一个出价 120 美元。在耳机上,有 50 美元和 80 美元的出价。

What I want to achieve is to have average bid per type.我想要实现的是每种类型的平均出价。 In this sample, that means i want to get 110 as average bid for watch and 65 as average bid for headphone.在此示例中,这意味着我希望获得 110 作为手表的平均出价和 65 作为耳机的平均出价。 To achieve that using T-SQL, I would write query like this:为了使用 T-SQL 实现这一点,我会编写如下查询:

SELECT t.name,
       avg(amount)
FROM bid b
LEFT JOIN offer o ON b.OfferId = o.id
LEFT JOIN offeritem oi ON o.id = oi.OfferId
LEFT JOIN itemType t ON oi.itemtypeid = t.Id
GROUP BY t.name

So, my question is - how to achieve that in dotnet core 3.0 EntityFramework所以,我的问题是 - 如何在 dotnet core 3.0 EntityFramework 中实现这一点

Using GroupBy, like this:使用 GroupBy,如下所示:

_context.Bids
    .Include(b => b.Offer)
        .ThenInclude(o => o.OfferItems)
            .ThenInclude(os => os.ItemType)
    .GroupBy(b => b.Offer.OfferItems.First().ItemType.Name);

gives exception:给出例外:

Client side GroupBy is not supported.不支持客户端 GroupBy。

. . When I try with projection, like this:当我尝试投影时,如下所示:

_context.Bids
    .Include(b => b.Offer)
        .ThenInclude(o => o.OfferItems)
            .ThenInclude(os => os.ItemType)
    .GroupBy(b => b.Offer.OfferItems.First().ItemType.Name)
    .Select(g => new
    {
        Key = g,
        Value = g.Average(b => b.Amount)
    });

i get exception again.我再次得到异常。

Processing of the LINQ.... failed. LINQ....的处理失败。 This may indicate either a bug or a limitation in EF Core.这可能表示 EF Core 中的错误或限制。

EDIT:编辑:

This approach这种方法

_context.Bids
    .Include(b => b.Offer)
        .ThenInclude(o => o.OfferItems)
            .ThenInclude(os => os.ItemType)
    .GroupBy(b => new { b.Offer.OfferItems.First().ItemType.Name}, b => b.Amount)
    .Select(g => new
    {
        Key = g.Key.Code,
        Value = g.Average()
    });

also threw an exception, but this time:也抛出了异常,但这次:

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.不能在用于 GROUP BY 子句的 group by 列表的表达式中使用聚合或子查询。

... ...

So, is there a way to group that data (get simple Average) or should I make another query and iterate throught collection and make calculation myself?那么,有没有办法对数据进行分组(获得简单的平均值),还是我应该进行另一个查询并遍历集合并自己进行计算? That would lower performance for sure (I was hoping I can do server grouping, but as you can see, i got into mentioned issues).这肯定会降低性能(我希望我可以进行服务器分组,但正如你所看到的,我遇到了提到的问题)。 Any ideas?有任何想法吗? Thanks in advance.提前致谢。

In your case it is hard to hide subquery from grouping在您的情况下,很难从分组中隐藏子查询

You can try it in such way你可以这样试试

var joined =
    context.Bid
        .SelectMany(x =>
            x.Offer.OfferItem
                .Select(y => new
                {
                    Amount = x.Amount,
                    Name = y.ItemType.Name
                })
                .Take(1));

var grouped = from i in joined
    group i by i.Name into groups
    select new
    {
        Key = groups.Key,
        Amount = groups.Average(x => x.Amount)
    };

it gives me a query它给了我一个查询

SELECT [t].[Name] AS [Key], AVG([t].[Amount]) AS [Amount]
  FROM [Bid] AS [b]
  INNER JOIN [Offer] AS [o] ON [b].[OfferId] = [o].[Id]
  CROSS APPLY (
      SELECT TOP(1) [b].[Amount], [i].[Name], [o0].[Id], [i].[Id] AS [Id0], [o0].[OfferId]
      FROM [OfferItem] AS [o0]
      INNER JOIN [ItemType] AS [i] ON [o0].[ItemTypeId] = [i].[Id]
      WHERE [o].[Id] = [o0].[OfferId]
  ) AS [t]
  GROUP BY [t].[Name]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Entity Framework Core 中更新多对多关系中的表? - How to update tables in many-to-many relationship in Entity Framework Core? 如何从 Entity Framework Core 构建的多对多关系中获取数据? - How to get data from many-to-many relationship builded by Entity Framework Core? Entity Framework Core:使用包含从相关表中获取数据 - Entity Framework Core : get data from related tables with include 在 Entity Framework Core MVC 应用程序中公开连接表中的数据 - Exposing data from connected tables in Entity Framework Core MVC app 多对多 Entity Framework Core 如何插入 - Many to many Entity Framework Core how to insert 如何使用Entity Framework从具有一对多关系的两个表中选择所有相关数据? - How to select all related data from two tables having one-to-many relationship using Entity Framework? 为什么 Entity Framework Core 试图将记录从多对多关系而不是连接表插入到其中一个表中? - Why is Entity Framework Core attempting to insert records into one of the tables from many to many relationships and NOT the join table? 如何从两个表中获取数据并将其保存在Net Core Entity Framework的DTO中? - How to get data from two tables and save it in DTO in Net Core Entity Framework? 如何使用 Entity Framework Core 在 .NET Core WebAPI 中返回嵌套表(一对多)? - How to return nested tables (one-to-many) in .NET Core WebAPI using Entity Framework Core? .NET Core Entity Framework 如何从模型创建表 - How does .NET Core Entity Framework Create Tables From Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM