question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot translate nested anonymous type

See original GitHub issue

Cannot translate

{ count = new { count = ctx.OrderItems.Where(orderItem => Order.Id == orderItem.OrderId).Count() } }
Exception message: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Stack trace:
This exception was originally thrown at this call stack:
    LinqToDB.Linq.Builder.ExpressionBuilder.EnforceServerSide(LinqToDB.Linq.Builder.IBuildContext)
    LinqToDB.Linq.Builder.ExpressionBuilder.TransformExpression(LinqToDB.Linq.Builder.IBuildContext, System.Linq.Expressions.Expression, bool, string)
    LinqToDB.Linq.Builder.ExpressionBuilder.BuildExpression.AnonymousMethod__0(System.Linq.Expressions.Expression)
    LinqToDB.Expressions.Extensions.Transform(System.Linq.Expressions.Expression, System.Func<System.Linq.Expressions.Expression, LinqToDB.Expressions.TransformInfo>)
    LinqToDB.Linq.Builder.ExpressionBuilder.BuildExpression(LinqToDB.Linq.Builder.IBuildContext, System.Linq.Expressions.Expression, bool, string)
    LinqToDB.Linq.Builder.SelectContext.BuildExpression.AnonymousMethod__2(System.Linq.Expressions.Expression)
    LinqToDB.Expressions.Extensions.Transform(System.Linq.Expressions.Expression, System.Func<System.Linq.Expressions.Expression, System.Linq.Expressions.Expression>)
    LinqToDB.Expressions.Extensions.TransformX(System.Linq.Expressions.BinaryExpression, System.Func<System.Linq.Expressions.Expression, System.Linq.Expressions.Expression>)
    LinqToDB.Expressions.Extensions.Transform(System.Linq.Expressions.Expression, System.Func<System.Linq.Expressions.Expression, System.Linq.Expressions.Expression>)
    LinqToDB.Expressions.Extensions.TransformX(System.Linq.Expressions.LambdaExpression, System.Func<System.Linq.Expressions.Expression, System.Linq.Expressions.Expression>)
    ...
    [Call Stack Truncated]

Steps to reproduce

using LinqToDB;
using LinqToDB.Mapping;
using System.Linq;

namespace OdataToEntity.Test.Linq2Db
{
    [Table(Schema = "dbo", Name = "Orders")]
    public class Order
    {
        [PrimaryKey, Identity] public int Id { get; set; }
        [Column, NotNull] public int CustomerId { get; set; }
    }

    [Table(Schema = "dbo", Name = "Customers")]
    public class Customer
    {
        [PrimaryKey, Identity] public int Id { get; set; }
    }

    [Table(Schema = "dbo", Name = "OrderItems")]
    public class OrderItem
    {
        [PrimaryKey, Identity] public int Id { get; set; }
        [Column, NotNull] public int OrderId { get; set; }
    }

    public partial class OdataToEntityDB : LinqToDB.Data.DataConnection
    {
        public OdataToEntityDB()
            : base("Microsoft.Data.SqlClient", @"Server=.\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;")
        {
        }

        public ITable<Customer> Customers { get { return this.GetTable<Customer>(); } }
        public ITable<Order> Orders { get { return this.GetTable<Order>(); } }
        public ITable<OrderItem> OrderItems { get { return this.GetTable<OrderItem>(); } }
    }

    class Program
    {
        static void Main()
        {
            using var ctx = new OdataToEntityDB();
            ctx.Orders
                .Select(
                    Order => new
                    {
                        Order,
                        count = new { count = ctx.OrderItems.Where(orderItem => Order.Id == orderItem.OrderId).Count() }
                    })
                .GroupJoin(
                    ctx.Customers,
                    order => order.Order.CustomerId,
                    customer => customer.Id,
                    (order, customer) => new { order, customer })
                .SelectMany(
                    a => a.customer.DefaultIfEmpty(),
                    (order, customer) => new { order.order.Order, order.order.count, customer }).ToList();
        }
    }
}

Environment details

linq2db version: 3.3.0 Database Server: SqlServer Express 14.0 Database Provider: Microsoft.Data.SqlClient Operating system: Windows Pro 20H2 .NET Framework: 5.0.300

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
voronov-maximcommented, Jun 1, 2021

Work fine in 3.3.1-rc.6015

0reactions
sdanylivcommented, Jun 1, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# Converting an anonymous object with nested collection ...
1 Answer. If you don't have a static type of a class you can't cast to it. For anonymous types having its type...
Read more >
C# Anonymous Types
An anonymous type's property can include another anonymous type. Example: Nested Anonymous Type. var student = new { Id = 1, FirstName =...
Read more >
Convert Anonymous to Named Type refactoring
Right-click and choose Refactor | Replace Anonymous Type with Named Class from the context menu. Choose ReSharper | Refactor | Convert | ...
Read more >
Using non anonymous type inside outer/inner key selector ...
But if I use non anonymous type CompositeKey in both key selectors it causes an error (The LINQ expression could not be translated)....
Read more >
Anonymous Type In C#
In above example we defined an anonymous type that is “Anonymous_Type” and this anonymous type contain a nested anonymous type “Name”. type.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found