Skip to content
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

Info on GroupBy #11887

Closed
berets76 opened this issue May 3, 2018 · 11 comments
Closed

Info on GroupBy #11887

berets76 opened this issue May 3, 2018 · 11 comments

Comments

@berets76
Copy link

berets76 commented May 3, 2018

Actually (2.1.o-preview2-final) this query

context.MyEntitityA
.GroupBy(g => new { g.fieldA, g.fieldB, g.fieldC }, (key, data) => data.OrderBy(o => o.fieldDate).FirstOrDefault())
.Where(w => w.fieldA == "" && w.fieldB == 2018 && w.fieldX1 == 123456)
.Select(...)
.ToList()

is not generated properly good, is it correct ?
Or is there another syntax compared to EF6 (origin of this query) ?

Further technical details

EF Core version: 2.1.0-preview2-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 64 bit
IDE: Visual Studio 2017 15.7.0 Preview 5

@smitpatel
Copy link
Member

@berets76 - What do you mean by "not generated properly good"? Can you elaborate what is current output and what is the output you are expecting?

@berets76
Copy link
Author

berets76 commented May 4, 2018

@smitpatel I'm expecting a GROUP BY clause or at least a single query with joins for navigation properties, instead I've the following output: tons of warning about locally evaluated FirstOrDefault and different queries for each related property I access.

Test case

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
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())
            {
                // Run queries
                Guid TenantID = Guid.Parse("6e359223-1b33-c541-80a2-0003ee5302d4");
                Guid CompanyID = Guid.Parse("ca74311d-6211-e001-80d2-0003ac5302d4");
                string Filter = string.Empty;

                var data = db.Set<GuestMovement>()
                            .GroupBy(g => new { g.GuestID }, (key, dat) => dat.OrderByDescending(o => o.When).FirstOrDefault())
                            .Where(w => w.TenantID == TenantID)
                            .Select(s => new FacilityGuestViewModel()
                            {
                                When = s.When,
                                ID = s.ID,
                                GuestID = s.GuestID,
                                Name = s.Guest.First + " " + s.Guest.Last,
                                VAT = s.Guest.VAT
                            })
                            .ToList();
                Console.WriteLine("Results count : " + data.Count.ToString());
            }
            
            Console.WriteLine("Program finished.");
            Console.ReadLine();
        }
    }


    public class MyContext : DbContext
    {
        private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);

        public string LanguageID => "it";

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connection = @"Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydatabase;Persist Security Info=False;User ID=myuser;Password=mypwd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
            optionsBuilder
                .UseSqlServer(connection)
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(LoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure model
            // Country
            modelBuilder.Entity<Country>().HasKey(c => new { c.TenantID, c.ID });
            modelBuilder.Entity<Country>().Property(p => p.ID).ValueGeneratedOnAdd().HasDefaultValueSql("newsequentialid()");
            // CountryName
            modelBuilder.Entity<CountryName>().HasKey(c => new { c.TenantID, c.CountryID, c.LanguageID });
            modelBuilder.Entity<CountryName>().HasOne(ho => ho.Country).WithMany(wm => wm.Names).HasForeignKey(fk => new { fk.TenantID, fk.CountryID }).OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<CountryName>().HasQueryFilter(qf => qf.LanguageID == LanguageID);
            // City
            modelBuilder.Entity<City>().HasKey(c => new { c.TenantID, c.CountryID, c.ID });
            modelBuilder.Entity<City>().Property(p => p.ID).ValueGeneratedOnAdd().HasDefaultValueSql("newsequentialid()");
            // CityName
            modelBuilder.Entity<CityName>().HasKey(c => new { c.TenantID, c.CountryID, c.CityID, c.LanguageID });
            modelBuilder.Entity<CityName>().HasOne(ho => ho.City).WithMany(wm => wm.Names).HasForeignKey(fk => new { fk.TenantID, fk.CountryID, fk.CityID }).OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<CityName>().HasQueryFilter(qf => qf.LanguageID == LanguageID);
            // Guest
            modelBuilder.Entity<Guest>().HasKey(c => new { c.TenantID, c.ID });
            modelBuilder.Entity<Guest>().Property(p => p.ID).ValueGeneratedOnAdd().HasDefaultValueSql("newsequentialid()");
            modelBuilder.Entity<Guest>().HasOne(ho => ho.BirthCountry).WithMany().HasForeignKey(fk => new { fk.TenantID, fk.BirthCountryID }).OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<Guest>().HasOne(ho => ho.BirthCity).WithMany().HasForeignKey(fk => new { fk.TenantID, fk.BirthCountryID, fk.BirthCityID }).OnDelete(DeleteBehavior.Restrict);
            // GuestMovement
            modelBuilder.Entity<GuestMovement>().HasKey(c => new { c.TenantID, c.CompanyID, c.GuestID, c.ID });
            modelBuilder.Entity<GuestMovement>().Property(p => p.ID).ValueGeneratedOnAdd().HasDefaultValueSql("newsequentialid()");
            modelBuilder.Entity<GuestMovement>().HasOne(ho => ho.Guest).WithMany().HasForeignKey(fk => new { fk.TenantID, fk.GuestID }).OnDelete(DeleteBehavior.Restrict);
        }
    }

    [Table("Guests")]
    public class Guest
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid ID { get; set; }
        public Guid? BirthCountryID { get; set; }
        public virtual Country BirthCountry { get; set; }
        public Guid? BirthCityID { get; set; }
        public virtual City BirthCity { get; set; }
        public DateTime? Cancelled { get; set; }
        public string VAT { get; set; }
        public string First { get; set; }
        public string Last { get; set; }
        public DateTime BirthDate { get; set; }
        public string ResidenceAddress { get; set; }
        [NotMapped]
        public string FullName => First + " " + Last;
        public bool IsHospitalized { get; set; }
        public bool IsPresent { get; set; }
        public short ReservationsNumber { get; set; }
        public string FacilityCode { get; set; }
    }
    [Table("Countries")]
    public class Country
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid ID { get; set; }

        public virtual List<CountryName> Names { get; set; }
    }
    [Table("CountryNames")]
    public class CountryName
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid CountryID { get; set; }
        [Key, Column(Order = 2)]
        [StringLength(3)]
        public string LanguageID { get; set; }
        [StringLength(255)]
        public string Name { get; set; }

        public virtual Country Country { get; set; }
    }
    [Table("Cities")]
    public class City
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid CountryID { get; set; }
        [Key, Column(Order = 2)]
        public Guid ID { get; set; }

        public virtual List<CityName> Names { get; set; }
    }
    [Table("CityNames")]
    public class CityName
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid CountryID { get; set; }
        [Key, Column(Order = 2)]
        public Guid CityID { get; set; }
        [Key, Column(Order = 3)]
        [StringLength(3)]
        public string LanguageID { get; set; }
        [StringLength(255)]
        public string Name { get; set; }

        public virtual City City { get; set; }
    }
    [Table("GuestMovements")]
    public class GuestMovement
    {
        [Key, Column(Order = 0)]
        public Guid TenantID { get; set; }
        [Key, Column(Order = 1)]
        public Guid CompanyID { get; set; }
        [Key, Column(Order = 2)]
        public Guid GuestID { get; set; }
        [Key, Column(Order = 3)]
        public Guid ID { get; set; }
        public DateTime When { get; set; }
        public virtual Guest Guest { get; set; }
        public DateTime? Cancelled { get; set; }
    }
    public class FacilityGuestViewModel
    {
        public DateTime When { get; set; }
        public Guid ID { get; set; }
        public Guid GuestID { get; set; }
        public string VAT { get; set; }
        public string Name { get; set; }
        public string AssignedPlace { get; set; }
    }
}

Output

dbug: Microsoft.EntityFrameworkCore.Infrastructure[10401]
      An 'IServiceProvider' was created for internal use by Entity Framework.
dbug: Microsoft.EntityFrameworkCore.Model[10601]
      The index {'TenantID', 'BirthCountryID'} was not created as the properties are already covered by the index {'TenantID', 'BirthCountryID', 'BirthCityID'}.
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.0-preview2-30571 initialized 'MyContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: SensitiveDataLoggingEnabled
dbug: Microsoft.EntityFrameworkCore.Query[10101]
      Compiling query model:
      'from IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w in
          (from GuestMovement g in DbSet<GuestMovement>
          select [g]).GroupBy(new <>f__AnonymousType0<Guid>([g].GuestID), [g])
      where
          (from GuestMovement o in [w]
          order by [o].When desc
          select [o]).FirstOrDefault().TenantID == __TenantID_0
      select new FacilityGuestViewModel{
          When =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().When,
          ID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().ID,
          GuestID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().GuestID,
          Name =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.First + " " +
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.Last,
          VAT =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.VAT
      }
      '
dbug: Microsoft.EntityFrameworkCore.Query[10104]
      Optimized query model:
      'from IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w in
          (from GuestMovement g in DbSet<GuestMovement>
          select [g]).GroupBy(new <>f__AnonymousType0<Guid>([g].GuestID), [g])
      where
          (from GuestMovement o in [w]
          order by [o].When desc
          select [o].TenantID).FirstOrDefault() == __TenantID_0
      select new FacilityGuestViewModel{
          When =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o].When).FirstOrDefault(),
          ID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o].ID).FirstOrDefault(),
          GuestID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o].GuestID).FirstOrDefault(),
          Name =
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].First).FirstOrDefault() + " " +
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].Last).FirstOrDefault(),
          VAT =
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].VAT).FirstOrDefault()
      }
      '
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'GroupBy(new <>f__AnonymousType0`1(GuestID = [g].GuestID), [g])' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ((Convert({from GuestMovement o in [w] orderby [o].When desc select [o].TenantID => FirstOrDefault()}, Nullable`1) ?? 00000000-0000-0000-0000-000000000000) == __TenantID_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
dbug: Microsoft.EntityFrameworkCore.Query[10107]
      (QueryContext queryContext) => IEnumerable<FacilityGuestViewModel> _InterceptExceptions(
          source: IEnumerable<FacilityGuestViewModel> _Select(
              source: IEnumerable<IGrouping<<>f__AnonymousType0<Guid>, GuestMovement>> _Where(
                  source: IEnumerable<IGrouping<<>f__AnonymousType0<Guid>, GuestMovement>> _GroupBy(
                      source: IEnumerable<GuestMovement> _ShapedQuery(
                          queryContext: queryContext,
                          shaperCommandContext: SelectExpression:
                              SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
                              FROM [GuestMovements] AS [g]
                              ORDER BY [g].[GuestID],
                          shaper: BufferedEntityShaper<GuestMovement>),
                      keySelector: (GuestMovement g) => new <>f__AnonymousType0<Guid>(g.GuestID),
                      elementSelector: (GuestMovement g) => g),
                  predicate: (IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w) => (Nullable<Guid>)Guid FirstOrDefault(IEnumerable<Guid> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => o.TenantID)) ?? 00000000-0000-0000-0000-000000000000 == Guid GetParameterValue(
                      queryContext: queryContext,
                      parameterName: "__TenantID_0")),
              selector: (IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w) => new FacilityGuestViewModel{
                  When = (Nullable<DateTime>)DateTime FirstOrDefault(IEnumerable<DateTime> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => o.When)) ?? 01/01/0001 00:00:00,
                  ID = (Nullable<Guid>)Guid FirstOrDefault(IEnumerable<Guid> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => o.ID)) ?? 00000000-0000-0000-0000-000000000000,
                  GuestID = (Nullable<Guid>)Guid FirstOrDefault(IEnumerable<Guid> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => o.GuestID)) ?? 00000000-0000-0000-0000-000000000000,
                  Name = string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
                                          FROM [Guests] AS [o.Guest3],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.First))) + " " + string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
                                          FROM [Guests] AS [o.Guest5],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.Last))),
                  VAT = string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
                                          FROM [Guests] AS [o.Guest7],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.VAT)))
              }
          ),
          contextType: EFSampleApp.MyContext,
          logger: DiagnosticsLogger<Query>,
          queryContext: queryContext)
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
      FROM [GuestMovements] AS [g]
      ORDER BY [g].[GuestID]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (136ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
      FROM [GuestMovements] AS [g]
      ORDER BY [g].[GuestID]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (64ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (60ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (68ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (58ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (58ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (71ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
Results count : 2
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
      'MyContext' disposed.
Program finished.

@berets76
Copy link
Author

berets76 commented May 4, 2018

Update

If you change the .Select adding nested navigation properties (Guest.BirthCity)

var data = db.Set<GuestMovement>()
                .GroupBy(g => new { g.GuestID }, (key, dat) => dat.OrderByDescending(o => o.When).FirstOrDefault())
                .Where(w => w.TenantID == TenantID)
                .Select(s => new FacilityGuestViewModel()
                {
                    When = s.When,
                    ID = s.ID,
                    GuestID = s.GuestID,
                    Name = s.Guest.First + " " + s.Guest.Last,
                    VAT = s.Guest.VAT,
                    BirthCityName = s.Guest.BirthCity.Names.FirstOrDefault().Name
                })
                .ToList();

you'll got an Exception at runtime

image

Stacktrace

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Lookup2.CreateForJoin(IEnumerable1 source, Func2 keySelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.JoinIterator[TOuter,TInner,TKey,TResult](IEnumerable1 outer, IEnumerable1 inner, Func2 outerKeySelector, Func2 innerKeySelector, Func3 resultSelector, IEqualityComparer1 comparer)+MoveNext() at System.Linq.OrderedEnumerable1.TryGetFirst(Boolean& found)
at System.Linq.Enumerable.SelectIPartitionIterator2.TryGetFirst(Boolean& found) at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at lambda_method(Closure , IGrouping2 )
at System.Linq.Enumerable.SelectEnumerableIterator2.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

It seems that lazy loading was ignored.

@smitpatel
Copy link
Member

FirstOrDefault on IGrouping does not translate to server at this point.

For the second query & exception you posted. I attempted to create a repro with your code above. https://gist.github.com/smitpatel/69ee32380b31ce9a7c41eae7506c63e5
I am not getting any exception. It may be affected by data in database. Can you please modify the repro code to demonstrate issue you are seeing.

@berets76
Copy link
Author

berets76 commented May 7, 2018

@smitpatel simply change the query to

var data = db.Set<GuestMovement>()
			.GroupBy(g => new { g.GuestID }, (key, dat) => dat.OrderByDescending(o => o.When).FirstOrDefault())
			.Where(w => w.TenantID == TenantID)
			.Select(s => new FacilityGuestViewModel()
			{
				When = s.When,
				ID = s.ID,
				GuestID = s.GuestID,
				Name = s.Guest.First + " " + s.Guest.Last,
				VAT = s.Guest.VAT,
				AssignedPlace = s.Guest.BirthCountry.Names.FirstOrDefault().Name
			})
			.ToList();

But if you have no data no exception was thrown.
With data you'll get this output (at the end you can see fail and exception)

dbug: Microsoft.EntityFrameworkCore.Infrastructure[10401]
      An 'IServiceProvider' was created for internal use by Entity Framework.
dbug: Microsoft.EntityFrameworkCore.Model[10601]
      The index {'TenantID', 'BirthCountryID'} was not created as the properties are already covered by the index {'TenantID', 'BirthCountryID', 'BirthCityID'}.
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.0-rc1-32029 initialized 'MyContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: SensitiveDataLoggingEnabled
dbug: Microsoft.EntityFrameworkCore.Query[10101]
      Compiling query model:
      'from IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w in
          (from GuestMovement g in DbSet<GuestMovement>
          select [g]).GroupBy(new <>f__AnonymousType0<Guid>([g].GuestID), [g])
      where
          (from GuestMovement o in [w]
          order by [o].When desc
          select [o]).FirstOrDefault().TenantID == __TenantID_0
      select new FacilityGuestViewModel{
          When =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().When,
          ID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().ID,
          GuestID =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().GuestID,
          Name =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.First + " " +
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.Last,
          VAT =
              (from GuestMovement o in [w]
              order by [o].When desc
              select [o]).FirstOrDefault().Guest.VAT,
          AssignedPlace =
              (from CountryName <generated>_1 in
                  (from GuestMovement o in [w]
                  order by [o].When desc
                  select [o]).FirstOrDefault().Guest.BirthCountry.Names
              select [<generated>_1]).FirstOrDefault().Name
      }
      '
warn: Microsoft.EntityFrameworkCore.Query[10103]
      Query: '(from CountryName <generated>_1 in  (from GuestMovement o in [w] order by [o].When desc select [o].G...' uses First/FirstOrDefault/Last/LastOrDefault operation without OrderBy and filter which may lead to unpredictable results.
dbug: Microsoft.EntityFrameworkCore.Query[10104]
      Optimized query model:
      'from IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w in
          (from GuestMovement g in DbSet<GuestMovement>
          select [g]).GroupBy(new <>f__AnonymousType0<Guid>([g].GuestID), [g])
      where (Guid)
          (from GuestMovement o in [w]
          order by [o].When desc
          select (Nullable<Guid>)[o].TenantID).FirstOrDefault() == __TenantID_0
      select new FacilityGuestViewModel{
          When = (DateTime)
              (from GuestMovement o in [w]
              order by [o].When desc
              select (Nullable<DateTime>)[o].When).FirstOrDefault(),
          ID = (Guid)
              (from GuestMovement o in [w]
              order by [o].When desc
              select (Nullable<Guid>)[o].ID).FirstOrDefault(),
          GuestID = (Guid)
              (from GuestMovement o in [w]
              order by [o].When desc
              select (Nullable<Guid>)[o].GuestID).FirstOrDefault(),
          Name =
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].First).FirstOrDefault() + " " +
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].Last).FirstOrDefault(),
          VAT =
              (from GuestMovement o in [w]
              join Guest o.Guest in DbSet<Guest>
              on new AnonymousObject(new object[]
                  {
                      (object)Property([o], "TenantID"),
                      (object)Property([o], "GuestID")
                  }) equals new AnonymousObject(new object[]
                  {
                      (object)Property([o.Guest], "TenantID"),
                      (object)Property([o.Guest], "ID")
                  })
              order by [o].When desc
              select [o.Guest].VAT).FirstOrDefault(),
          AssignedPlace =
              (from CountryName <generated>_1 in
                  (from GuestMovement o in [w]
                  join Guest o.Guest in DbSet<Guest>
                  on new AnonymousObject(new object[]
                      {
                          (object)Property([o], "TenantID"),
                          (object)Property([o], "GuestID")
                      }) equals new AnonymousObject(new object[]
                      {
                          (object)Property([o.Guest], "TenantID"),
                          (object)Property([o.Guest], "ID")
                      })
                  order by [o].When desc
                  select [o.Guest].BirthCountry).FirstOrDefault().Names
              select [<generated>_1].Name).FirstOrDefault()
      }
      '
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'GroupBy(new <>f__AnonymousType0`1(GuestID = [g].GuestID), [g])' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where (Convert({from GuestMovement o in [w] orderby [o].When desc select Convert([o].TenantID, Nullable`1) => FirstOrDefault()}, Guid) == __TenantID_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join Guest o.Guest in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFSampleApp.Guest]) on new AnonymousObject(new [] {Convert(Property([o], "TenantID"), Object), Convert(Property([o], "GuestID"), Object)}) equals new AnonymousObject(new [] {Convert(Property([o.Guest], "TenantID"), Object), Convert(Property([o.Guest], "ID"), Object)})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [o].When desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
dbug: Microsoft.EntityFrameworkCore.Query[10107]
      (QueryContext queryContext) => IEnumerable<FacilityGuestViewModel> _InterceptExceptions(
          source: IEnumerable<FacilityGuestViewModel> _Select(
              source: IEnumerable<IGrouping<<>f__AnonymousType0<Guid>, GuestMovement>> _Where(
                  source: IEnumerable<IGrouping<<>f__AnonymousType0<Guid>, GuestMovement>> _GroupBy(
                      source: IEnumerable<GuestMovement> _ShapedQuery(
                          queryContext: queryContext,
                          shaperCommandContext: SelectExpression:
                              SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
                              FROM [GuestMovements] AS [g]
                              ORDER BY [g].[GuestID],
                          shaper: BufferedEntityShaper<GuestMovement>),
                      keySelector: (GuestMovement g) => new <>f__AnonymousType0<Guid>(g.GuestID),
                      elementSelector: (GuestMovement g) => g),
                  predicate: (IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w) => (Guid)Nullable<Guid> FirstOrDefault(IEnumerable<Nullable<Guid>> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => (Nullable<Guid>)o.TenantID)) == Guid GetParameterValue(
                      queryContext: queryContext,
                      parameterName: "__TenantID_0")),
              selector: (IGrouping<<>f__AnonymousType0<Guid>, GuestMovement> w) => new FacilityGuestViewModel{
                  When = (DateTime)Nullable<DateTime> FirstOrDefault(IEnumerable<Nullable<DateTime>> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => (Nullable<DateTime>)o.When)),
                  ID = (Guid)Nullable<Guid> FirstOrDefault(IEnumerable<Nullable<Guid>> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => (Nullable<Guid>)o.ID)),
                  GuestID = (Guid)Nullable<Guid> FirstOrDefault(IEnumerable<Nullable<Guid>> _Select(
                          source: IOrderedEnumerable<GuestMovement> _OrderBy(
                              source: w,
                              expression: (GuestMovement o) => o.When,
                              orderingDirection: Desc),
                          selector: (GuestMovement o) => (Nullable<Guid>)o.GuestID)),
                  Name = string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
                                          FROM [Guests] AS [o.Guest3],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.First))) + " " + string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
                                          FROM [Guests] AS [o.Guest5],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.Last))),
                  VAT = string FirstOrDefault(IEnumerable<string> _Select(
                          source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _OrderBy(
                              source: IEnumerable<TransparentIdentifier<GuestMovement, ValueBuffer>> _Join(
                                  outer: w,
                                  inner: IEnumerable<ValueBuffer> _ShapedQuery(
                                      queryContext: queryContext,
                                      shaperCommandContext: SelectExpression:
                                          SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
                                          FROM [Guests] AS [o.Guest7],
                                      shaper: ValueBufferShaper),
                                  outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o),
                                          (object)Guid GetValueFromEntity(
                                              clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                              entity: o)
                                      }),
                                  innerKeySelector: (ValueBuffer o.Guest) => new AnonymousObject(new object[]
                                      {
                                          (object)Guid TryReadValue(o.Guest, 0, Guest.TenantID),
                                          (object)Guid TryReadValue(o.Guest, 1, Guest.ID)
                                      }),
                                  resultSelector: (GuestMovement o | ValueBuffer o.Guest) => TransparentIdentifier<GuestMovement, ValueBuffer> CreateTransparentIdentifier(
                                      outer: o,
                                      inner: o.Guest)),
                              expression: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => t0.Outer.When,
                              orderingDirection: Desc),
                          selector: (TransparentIdentifier<GuestMovement, ValueBuffer> t0) => string TryReadValue(t0.Inner, 2, Guest.VAT))),
                  AssignedPlace = string FirstOrDefault(IEnumerable<string> _Select(
                          source: Country FirstOrDefault(IEnumerable<Country> _Select(
                                  source: IOrderedEnumerable<TransparentIdentifier<GuestMovement, Guest>> _OrderBy(
                                      source: IEnumerable<TransparentIdentifier<GuestMovement, Guest>> _Join(
                                          outer: w,
                                          inner: IEnumerable<Guest> _ShapedQuery(
                                              queryContext: queryContext,
                                              shaperCommandContext: SelectExpression:
                                                  SELECT [o.Guest9].[TenantID], [o.Guest9].[ID], [o.Guest9].[BirthCityID], [o.Guest9].[BirthCountryID], [o.Guest9].[BirthDate], [o.Guest9].[Cancelled], [o.Guest9].[FacilityCode], [o.Guest9].[First], [o.Guest9].[IsHospitalized], [o.Guest9].[IsPresent], [o.Guest9].[Last], [o.Guest9].[ReservationsNumber], [o.Guest9].[ResidenceAddress], [o.Guest9].[VAT], [o.Guest9].[Name]
                                                  FROM [Guests] AS [o.Guest9],
                                              shaper: BufferedEntityShaper<Guest>),
                                          outerKeySelector: (GuestMovement o) => new AnonymousObject(new object[]
                                              {
                                                  (object)Guid GetValueFromEntity(
                                                      clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                                      entity: o),
                                                  (object)Guid GetValueFromEntity(
                                                      clrPropertyGetter: ClrPropertyGetter<GuestMovement, Guid>,
                                                      entity: o)
                                              }),
                                          innerKeySelector: (Guest o.Guest) => new AnonymousObject(new object[]
                                              {
                                                  (object)Guid GetValueFromEntity(
                                                      clrPropertyGetter: ClrPropertyGetter<Guest, Guid>,
                                                      entity: o.Guest),
                                                  (object)Guid GetValueFromEntity(
                                                      clrPropertyGetter: ClrPropertyGetter<Guest, Guid>,
                                                      entity: o.Guest)
                                              }),
                                          resultSelector: (GuestMovement o | Guest o.Guest) => TransparentIdentifier<GuestMovement, Guest> CreateTransparentIdentifier(
                                              outer: o,
                                              inner: o.Guest)),
                                      expression: (TransparentIdentifier<GuestMovement, Guest> t0) => t0.Outer.When,
                                      orderingDirection: Desc),
                                  selector: (TransparentIdentifier<GuestMovement, Guest> t0) => t0.Inner.BirthCountry)).Names,
                          selector: (CountryName <generated>_1) => <generated>_1.Name))
              }
          ),
          contextType: EFSampleApp.MyContext,
          logger: DiagnosticsLogger<Query>,
          queryContext: queryContext)
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
      FROM [GuestMovements] AS [g]
      ORDER BY [g].[GuestID]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (133ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [g].[TenantID], [g].[CompanyID], [g].[GuestID], [g].[ID], [g].[Cancelled], [g].[When]
      FROM [GuestMovements] AS [g]
      ORDER BY [g].[GuestID]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (67ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest3].[TenantID], [o.Guest3].[ID], [o.Guest3].[First]
      FROM [Guests] AS [o.Guest3]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (81ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest5].[TenantID], [o.Guest5].[ID], [o.Guest5].[Last]
      FROM [Guests] AS [o.Guest5]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (63ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest7].[TenantID], [o.Guest7].[ID], [o.Guest7].[VAT]
      FROM [Guests] AS [o.Guest7]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest9].[TenantID], [o.Guest9].[ID], [o.Guest9].[BirthCityID], [o.Guest9].[BirthCountryID], [o.Guest9].[BirthDate], [o.Guest9].[Cancelled], [o.Guest9].[FacilityCode], [o.Guest9].[First], [o.Guest9].[IsHospitalized], [o.Guest9].[IsPresent], [o.Guest9].[Last], [o.Guest9].[ReservationsNumber], [o.Guest9].[ResidenceAddress], [o.Guest9].[VAT], [o.Guest9].[Name]
      FROM [Guests] AS [o.Guest9]
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (68ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [o.Guest9].[TenantID], [o.Guest9].[ID], [o.Guest9].[BirthCityID], [o.Guest9].[BirthCountryID], [o.Guest9].[BirthDate], [o.Guest9].[Cancelled], [o.Guest9].[FacilityCode], [o.Guest9].[First], [o.Guest9].[IsHospitalized], [o.Guest9].[IsPresent], [o.Guest9].[Last], [o.Guest9].[ReservationsNumber], [o.Guest9].[ResidenceAddress], [o.Guest9].[VAT], [o.Guest9].[Name]
      FROM [Guests] AS [o.Guest9]
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Name'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:54241151-67f8-494f-8d32-1b8791f91370
Error Number:207,State:1,Class:16
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
      Closing connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
      Closed connection to database 'mydatabase' on server 'tcp:myserver.database.windows.net,1433'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred in the database while iterating the results of a query for context type 'EFSampleApp.MyContext'.
      System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Name'.
         at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
         at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
         at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
         at System.Data.SqlClient.SqlDataReader.get_MetaData()
         at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
         at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
         at System.Data.Sq

@berets76
Copy link
Author

berets76 commented May 7, 2018

p.s: I test it with 2.1.0-rc1-final today, same result

@smitpatel
Copy link
Member

The main issue here is this

AssignedPlace =
              (from CountryName <generated>_1 in
                  (from GuestMovement o in [w]
                  join Guest o.Guest in DbSet<Guest>
                  on new AnonymousObject(new object[]
                      {
                          (object)Property([o], "TenantID"),
                          (object)Property([o], "GuestID")
                      }) equals new AnonymousObject(new object[]
                      {
                          (object)Property([o.Guest], "TenantID"),
                          (object)Property([o.Guest], "ID")
                      })
                  order by [o].When desc
                  select [o.Guest].BirthCountry).FirstOrDefault().Names
              select [<generated>_1].Name).FirstOrDefault()

o -> GuestMovement
o.Guest -> Guest
o.Guest.BirthCountry is navigation but did not get nav expansion. (no join with Country)
Names is collection navigation to CountryName which did not get expanded either.
Hence while translating to SQL we used correct property with totally incorrect table causing invalid sql.

Assigning to @maumar

@berets76
Copy link
Author

@smitpatel regarding your first problem answer

FirstOrDefault on IGrouping does not translate to server at this point.

How can I, actually, get only first item from an ordered grouped query ?
Do I need to write TSQL and try with DbQuery and .FromSql?

Sample

public class Post
{
	[Key, Column(Order = 0)]
	public int BlogID { get; set; }
	[Key, Column(Order = 1)]
	public int ID { get; set; }
	public DateTime PostDate { get; set; }
	public string Title { get; set; }
        -omitted-
	public string Category { get; set; }
}

Filter posts by BlogID, grouping by Category, ordering descending by PostDate and retrieve only the latest (newest PostDate) item (with all its properties and navigation properties) for each Category.

Data

BlogID      ID	        PostDate               Title  		Category
1		1	01/01/2018 08:10:12    Post A		TECH
1		2	01/01/2018 08:10:12    Post B		TECH
1		3	01/01/2018 08:10:12    Post C		TECH
3		1	01/01/2018 08:10:12    Post 1		SPORT
3		2	01/01/2018 06:55:47    Post 2		TECH
3		3	01/01/2018 08:10:14    Post 3		SPORT
3		4	01/01/2018 07:23:02    Post 4		TECH
3		5	01/01/2018 08:10:12    Post 5		GOSSIP
3		6	01/01/2018 12:09:12    Post 6		GOSSIP

Expected results

3		4	01/01/2018 07:23:02    Post 4		TECH		2
3		3	01/01/2018 08:10:14    Post 3		SPORT		2
3		6	01/01/2018 12:09:12    Post 6		GOSSIP		2

Last column (with 2 value) is the count of grouped items.

I tried different ways but every time I got "locally executed" with a lot of small queries for each item.

@smitpatel
Copy link
Member

FromSql with QueryTypes is the simplest way to get the data.

@smitpatel
Copy link
Member

@smitpatel
Copy link
Member

Duplicate of #12088

@smitpatel smitpatel marked this as a duplicate of #12088 Dec 6, 2019
@smitpatel smitpatel removed this from the Backlog milestone Dec 6, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants