简体   繁体   English

C# LINQ Select JOIN GroupBy 不工作

[英]C# LINQ Select JOIN GroupBy is not working

I am using entityFramework Core and got a LINQ query select, inner join, where and got the results.我正在使用 entityFramework Core 并得到一个 LINQ 查询 select,内连接,在哪里得到了结果。 I need to add GroupBy and it an error.我需要添加 GroupBy 并且它是一个错误。 I followed a few posts and mimic their code but I have been stucked.我关注了一些帖子并模仿了他们的代码,但我被卡住了。

I am having issue with translating to LINQ我在翻译为 LINQ 时遇到问题

Here is my linq that is working without the GroupBy这是我的 linq 在没有 GroupBy 的情况下工作

var result = this.myDbContent.Table1
               .Join(this.Table2,
                     table1 => table1.myId,
                     table2 => table2.myId,
                     (table1 ,table2) => {table1 ,table2}
               )
               .Join(this.Table3,
                     table1_table2 => table1_table2.table1.myId,
                     table3 => table3.myId,
                     (table1_table2 ,table3) => {tabtable1_table2 e1 ,table3}
               )
               .Where(
                 s => s.table1_table2.table1.myId == 1 &&
                 s.table1_table2.table2.isCompleted == true
               )
               .Select(s => new
               {
                  MyID = s.table1_table2.table1.myId,
                  MyDate = s.table1_table2.Table2.CompletedDate
               });

This query works and returns records此查询有效并返回记录

In the following I added the GroupBy according to another post在下面我根据另一篇文章添加了 GroupBy

var result = this.myDbContent.Table1
               .Join(this.Table2,
                     table1 => table1.myId,
                     table2 => table2.myId,
                     (table1 ,table2) => {table1 ,table2}
               )
               .Join(this.Table3,
                     table1_table2 => table1_table2.table1.myId,
                     table3 => table3.myId,
                     (table1_table2 ,table3) => {tabtable1_table2 e1 ,table3}
               )
               .Where(
                 s => s.table1_table2.table1.myId == 1 &&
                 s.table1_table2.table2.isCompleted == true
               )
               .GroupBy(s => new
                 {
                    MyID = s.table1_table2.table1.myId,
                    MyDate = s.table1_table2.Table2.CompletedDate
                 }
               )
               .Select(s => new
               {
                  MyID = s.Key.myId,
                  MyDate = s.Key.CompletedDate,
                  Count = s.Count()
               });

When I run this query with the GroupBy, I get an"SYstem.InvalidOperationException当我使用 GroupBy 运行此查询时,我得到一个“SYStem.InvalidOperationException”

I am trying to add the following SELECT GROUPBY to the working QUERY我正在尝试将以下 SELECT GROUPBY 添加到工作查询中

Select Count(*) AS Counts, MyID, MYDATE
FROM ( the working query above )
GROUP BY MyID, MYDATE

Thanks谢谢

Yourgroup key only has MyID and MyDate fields - the references to the joined tables are gone, so reference those in your select:您的组键只有MyIDMyDate字段 - 对连接表的引用已消失,因此请参考 select 中的那些:

.Select(s => new
           {
              MyID = s.Key.MyID,
              MyDate = s.Key.MyDate,
              Count = s.Count()
           }

Convert it to a list or Enumerable before grouping在分组之前将其转换为列表或 Enumerable

var result = this.myDbContent.Table1
               .Join(this.Table2,
                     table1 => table1.myId,
                     table2 => table2.myId,
                     (table1 ,table2) => {table1 ,table2}
               )
               .Join(this.Table3,
                     table1_table2 => table1_table2.table1.myId,
                     table3 => table3.myId,
                     (table1_table2 ,table3) => {tabtable1_table2 e1 ,table3}
               )
               .Where(
                 s => s.table1_table2.table1.myId == 1 &&
                 s.table1_table2.table2.isCompleted == true
               ).AsEnumerable()
               .GroupBy(s => new
                 {
                    MyID = s.table1_table2.table1.myId,
                    MyDate = s.table1_table2.Table2.CompletedDate
                 }
               )
               .Select(s => new
               {
                  MyID = s.Key.table1_table2.table1.myId,
                  MyDate = s.Key.table1_table2.Table2.CompletedDate,
                  Count = s.Count()
               });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM