Open MoRooz opened 4 years ago
@MoRooz If possible, provide a full working sample project.
When calling ToArray()
on the q1 like:
var q1 = context.Blogs.GroupBy(context.Blogs.KeySelectorExpression(groupByList));
var q1a = q1.ToArray();
Already gives an exception:
System.InvalidOperationException: 'Client projection contains reference to constant expression of type: Microsoft.EntityFrameworkCore.Metadata.IPropertyBase. This could potentially cause memory leak.'
See also
When upgrading to 3.1 preview, I get this better error:
System.InvalidOperationException: 'Client projection contains reference to constant expression of 'Microsoft.EntityFrameworkCore.Metadata.IPropertyBase' which is being passed as argument to method 'TryReadValue'. This could potentially cause memory leak. Consider assigning this constant to local variable and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.'
memory leak is probably because of InMemory Provider, here is Sqlite one, with another error
Client side GroupBy is not supported
var options = new DbContextOptionsBuilder<BloggingContext>().UseSqlite("Filename=MyDatabase.db").Options;
using (var context = new BloggingContext(options))
{
context.Database.EnsureCreated();
context.Blogs.Add(new Blog { CategoryId = 1, PageId = 2 });
context.Blogs.Add(new Blog { CategoryId = 1, PageId = 3 });
context.Blogs.Add(new Blog { CategoryId = 2, PageId = 2 });
context.Blogs.Add(new Blog { CategoryId = 2, PageId = 2 });
context.SaveChanges();
// not working
var groupByList = new List<string> { "CategoryId" };
var q1 = context.Blogs.GroupBy(context.Blogs.KeySelectorExpression(groupByList));
//var q11List = q1.ToArray();
var q3 = System.Linq.Dynamic.Core.DynamicQueryableExtensions.GroupBy(context.Blogs, "CategoryId");
var q31List = q3.ToDynamicArray();
var q2 = q1.Select("new (Key.CategoryId as CategoryId, it.Count() as Count)");
//var q2 = q1.Select("new (it.Key.CategoryId as CategoryId, it.Count() as Count)");
var result0 = q2.ToDynamicList<object>();
// working
var g0 = context.Blogs.GroupBy("new (CategoryId)");
var g1 = g0.Select("new (it.Key.CategoryId as CategoryId, it.Count() as Count)");
var list = g1.ToDynamicList<object>();
}
both LinqGroupBy and DynamicLinq GroupBy has this error
seems some sql aggregate is necessary in select
https://github.com/aspnet/EntityFrameworkCore/issues/18102#issuecomment-536231179
this is working,
var groupByList = new List<string> { "CategoryId","PageId" };
var q2 = context.Blogs.GroupBy(context.Blogs.KeySelectorExpression(groupByList)).Select("new (it.Key as Key, it.Count() as Count)");
var q2List = q2.ToDynamicList<object>();
now only if its possible to get flatten key.
current
{{ Key = { CategoryId = 1, PageId = 2 }, Count = 34 }}
need (used to work in net 2.2 with Key.Property in select)
{{ CategoryId = 1, PageId = 2, Count = 34 }}
@MoRooz Thanks for researching. Would it be an idea to add this in a readable markdown format in a wiki? If so, can you please help.
this is source of exception
the same code works fine in .net core 2.2