Is Aggregate fatally flawed because each into clause is executed separately?

前端 未结 2 1468
遥遥无期
遥遥无期 2021-01-02 02:05

Is VB.NET\'s Aggregate query fatally flawed when used as the first (outer) clause of a Linq expression with multiple Into clauses because each

2条回答
  •  长发绾君心
    2021-01-02 02:44

    Although it doesn't use the Aggregate keyword, you can do multiple functions in a single query using the following syntax:

    Dim query = From book In books _
        Group By key = book.Subject Into Group _
        Select id = key, _
            BookCount = Group.Count, _
            TotalPrice = Group.Sum(Function(_book) _book.Price), _
            LowPrice = Group.Min(Function(_book) _book.Price), _
            HighPrice = Group.Max(Function(_book) _book.Price), _
            AveragePrice = Group.Average(Function(_book) _book.Price)
    

    There does appear to be an issue with the Aggregate clause implementation though. Consider the following query from Northwind:

    Aggregate o in Orders
    into Sum(o.Freight),
    Average(o.Freight),
    Max(o.Freight)
    

    This issues 3 database requests. The first two perform separate aggregate clauses. The third pulls the entire table back to the client and performs the Max on the client through Linq to Objects.

提交回复
热议问题