| Bug #82604 | LINQ conditional Group By column excluded from select list | ||
|---|---|---|---|
| Submitted: | 17 Aug 2016 3:41 | Modified: | 17 Aug 2016 6:36 |
| Reporter: | Brian Tacker | Email Updates: | |
| Status: | Duplicate | Impact on me: | |
| Category: | Connector / NET | Severity: | S3 (Non-critical) |
| Version: | 6.9.9 | OS: | Windows |
| Assigned to: | CPU Architecture: | Any | |
| Tags: | linq | ||
[17 Aug 2016 6:36]
Chiranjeevi Battula
Hello Brian Tacker, Thank you for the bug report. This is most likely duplicate of Bug #79817, please see Bug #79817. Thanks, Chiranjeevi.
[22 Feb 2021 11:00]
Christopher Leroi
Dear support, any updates on this issue? Is there a workaround available? Thank you very much! BR Chris
[1 Mar 2021 15:47]
Christopher Leroi
Workaround: use myGroup.Min(f => f.x) instead of myGroup.Key.x

Description: When doing a LINQ statement with a conditional group by, the group by column is not included in the SELECT list. How to repeat: The following LINQ statement: from trans in Transaction group trans by new { GroupCol = (groupByDate ? trans.Date : trans.StoreId) } into g1 select new { Group = g1.Key.GroupCol, Count = g1.Count() } Will produce the following SQL: SELECT 1 AS `C1`, `GroupBy1`.`K1` AS `C2`, `GroupBy1`.`A1` AS `C3` FROM (SELECT COUNT(1) AS `A1` FROM `Transactions` AS `Extent1` GROUP BY CASE WHEN (@p__linq__0 = 1) THEN (`Extent1`.`Date`) ELSE (`Extent1`.`StoreId`) END) AS `GroupBy1` When this SQL is run, you get "Unknown column 'GroupBy1.K1' in 'field list'" Suggested fix: Producing the following SQL gives the desired result: SELECT 1 AS `C1`, `GroupBy1`.`K1` AS `C2`, `GroupBy1`.`A1` AS `C3` FROM (SELECT CASE WHEN (@p__linq__0 = 1) THEN (`Extent1`.`Date`) ELSE (`Extent1`.`StoreId`) END AS `K1`, COUNT(1) AS `A1` FROM `Transactions` AS `Extent1` GROUP BY `K1`) AS `GroupBy1`