-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid query generated for simple group by #14243
Comments
I am not able to reproduce the issue based on above data. The exact same query is working fine for me. using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace EFSampleApp
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new MyContext())
{
// Recreate database
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Seed database
db.SaveChanges();
}
using (var db = new MyContext())
{
// Run queries
var ids = new List<int> { 1, 2, 3 };
var efQuery = from blog in db.Blogs
where ids.Contains(blog.Id)
group blog by new { blog.Id, blog.TpId } into g
select new NetAmountDto
{
Id = g.Key.Id,
TpId = g.Key.TpId,
NetAmount = g.Sum(s => s.Jul)
};
efQuery.ToList();
}
Console.WriteLine("Program finished.");
}
}
public class MyContext : DbContext
{
private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);
// Declare DBSets
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Select 1 provider
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
//.UseSqlite("filename=_modelApp.db")
//.UseInMemoryDatabase(databaseName: "_modelApp")
.EnableSensitiveDataLogging()
.UseLoggerFactory(LoggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure model
}
}
public class Blog
{
public int Id { get; set; }
public int TpId { get; set; }
public int Jul { get; set; }
}
public class NetAmountDto
{
public int Id { get; set; }
public int TpId { get; set; }
public int NetAmount { get; set; }
}
} Generated SQL: SELECT [blog].[Id], [blog].[TpId], SUM([blog].[Jul]) AS [NetAmount]
FROM [Blogs] AS [blog]
WHERE [blog].[Id] IN (1, 2, 3)
GROUP BY [blog].[Id], [blog].[TpId] |
Thank you for quick response. I have attached only relevant version of the code in solution where I was able to repro. The Query printed in (debug) output window is as follows: #Update: I tried putting your code in SqlLite it still does not work in my sample Another observation: And below generates a proper query with server side aggregation: |
@smitpatel to follow up. |
Duplicate of #14083 |
Duplicate of #12089 |
Describe what is not working as expected.
Simple group by generating invalid query and also warning that Sum getting evaluated in local
Exception message:
Issue 1
Above LINQ expression generates Invalid Query:
SELECT "shiftDetail"."ShiftId", "shiftDetail"."TpId", "shiftDetail"."Jul" AS "NetAmount"
FROM "ShiftDetail" AS "shiftDetail"
WHERE "shiftDetail"."ShiftId" IN (1000000, 1000100, 1000200)
GROUP BY "shiftDetail"."ShiftId", "shiftDetail"."TpId"
Column: "shiftDetail"."Jul" is not in Group By but in select
Issue 2
Sum should be evaluated on DB Server, EF core throws following warning
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Sum()' could not be translated and will be evaluated locally.
Steps to reproduce
Further technical details
EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlLite 2.2.0
Operating system: Win 10
IDE: Visual Studio 2017 15.9.4
The text was updated successfully, but these errors were encountered: