linq2db.EntityFrameworkCore icon indicating copy to clipboard operation
linq2db.EntityFrameworkCore copied to clipboard

LinqToDB Query Fails on GroupBY Entity Core works?

Open aloksharma1 opened this issue 3 years ago • 1 comments

hi please check this code :

await TranslatedSitesRepository.NoTrackingQuery().Where(x => x.IsActive == true && x.AttractionId == Id.ChangeType<Guid>() && x.LangCode == langCode).Select(x =>
                new
                {
                    Id = GId,
                    BannerImage = repository.NoTrackingQuery().Where(mr => mr.IsActive == true && mr.Id == GId).Select(mr => mr.BannerImage).FirstOrDefault(),
                    x.LangCode,
                    x.SiteInfo,
                    x.SiteSummary,
                    x.SiteTitle,
                    SiteFiles = FilesRepository.Query().Where(f => f.IsActive == true && f.isPublished && f.LangCode == langCode && f.AttractionId == GId).OrderBy(f => f.SortOrder).ToList(),
                    SiteLocation = TranslatedMapsRepository.Query().Where(m => m.IsActive == true && m.AttractionId == GId && m.LangCode == langCode).FirstOrDefault(),
                    timelineInfo = TranslatedTimelineInfoRepository.Query().Where(t => t.IsActive == true && t.AttractionId == GId && t.LangCode == langCode)
                    .GroupBy(t => t.GroupName)
                    .Select(t => new
                    {
                        t.Key,
                        timeline = t.Select(tx => new
                        {
                            tx.EventInfo,
                            tx.EventDate,
                            tx.SortOrder
                        }).OrderBy(tx => tx.SortOrder).ToList()
                    }).ToList(),
                    x.DateModified
                }).ToLinqToDB().FirstOrDefaultAsync();

it works without linqtodb() but with linqtodb I am getting following error:

An unhandled exception occurred while processing the request.
LinqToDBException: You should explicitly specify selected fields for server-side GroupBy() call or add AsEnumerable() call before GroupBy() to perform client-side grouping.
Set Configuration.Linq.GuardGrouping = false to disable this check.
Additionally this guard exception can be disabled by extension GroupBy(...).DisableGuard().
NOTE! By disabling this guard you accept additional Database Connection(s) to the server for processing such requests.
LinqToDB.Linq.Builder.GroupByBuilder+GroupByContext+GroupByHelper<TKey, TElement, TSource>.GetGrouping(GroupByContext context)

TargetInvocationException: Exception has been thrown by the target of an invocation.
System.RuntimeMethodHandle.InvokeMethod(object target, ref Span<object> arguments, Signature sig, bool constructor, bool wrapExceptions)

aloksharma1 avatar Aug 01 '22 09:08 aloksharma1

Such query support was added to EF Core 6. In linq2db we plan to support this in version 5.0.

sdanyliv avatar Aug 02 '22 06:08 sdanyliv