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.

IQueryable Extensions Exception

See original GitHub issue

I’ve been trying to implement a project with automapper for the first time, and I keep running into the same issue when writing queries and using the IQueriable extensions.

This is a simplified Dto model.

public class WidgetDto
{
    public int Id { get; set; }
    public string WidgetName { get; set; }
    public string Description { get; set; }
    public List<WidgetCategoryDto> WidgetCategories { get; set; }
}

public class CategoryDto
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public List<WidgetCategoryDto> WidgetCategories { get; set; }
}

public class WidgetCategoryDto
{
    public int Id { get; set; }
    public int WidgetId { get; set; }
    public int CategoryId { get; set; }
    public CategoryDto Category { get; set; }
    public WidgetDto Widget { get; set; }
}

With a very simple mapping configuration.

Mapper.CreateMap<Widget, WidgetDto>.MaxDepth(3);
Mapper.CreateMap<WidgetCategory, WidgetCategoryDto>.MaxDepth(3);
Mapper.CreateMap<Category, CategoryDto>.MaxDepth(3);

If I write a very simple query then the exception occurs.

var db = new DbContext();

var query = db.Widgets
    .Project()
    .To&lt;WidgetDto&gt;()
    .ToList();

Additional information: The type ‘Widget.WidgetCategoryDto’ appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

From what I’ve read, the expected behaviour is to eager load related mapped types to recursive depth of 3.

However, I can’t get the query to work without it throwing the above exception.

I would actually like to optionally expand sub types using a mapping configuration like so…

Mapper.CreateMap<Widget, WidgetDto>()
    .MaxDepth(3)
    .ForMember(dto => dto.WidgetCategories, opt => opt.ExplicitExpansion());

Mapper.CreateMap<WidgetCategory, WidgetCategoryDto>()
    .MaxDepth(3)
    .ForMember(dto => dto.Category, opt => opt.ExplicitExpansion())
    .ForMember(dto => dto.Widget, opt => opt.ExplicitExpansion());

Mapper.CreateMap<Category, CategoryDto>()
    .MaxDepth(3)
    .ForMember(dto => dto.WidgetCategories, opt => opt.ExplicitExpansion());

However, the following query also throws the same exception

var db = new DbContext();

var query = db.Widgets
    .Project()
    .To<WidgetDto>(null, 
         dto => dto.WidgetCategories, 
         dto => dto.WidgetCategories.Select(x => x.Category))
    .ToList();

Am I missing something?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerCarlson1commented, Jul 16, 2015

If you make your Dto objects like so

public class WidgetDto
{
public int Id { get; set; }
public string WidgetName { get; set; }
public string Description { get; set; }
public List<WidgetCategoryDto> WidgetCategories { get; set; }
}

public class CategoryDto
{
public int Id { get; set; }
public string CategoryName { get; set; }
//public List<WidgetCategoryDto> WidgetCategories { get; set; }
}

public class WidgetCategoryDto
{
public int Id { get; set; }
public int WidgetId { get; set; }
public int CategoryId { get; set; }
public CategoryDto Category { get; set; }
//public WidgetDto Widget { get; set; }
}

It should work. The problem is commented out properties will cause an infinite loop when trying to Project. Project makes a select statement, and DON’T use Include(), and you would have an infinitely long select statement.

IF you need those properties you need to make them separate Dto class for one level down and so on and so forth until you want to stop recursively going down the list, where you wouldn’t have the child property.

0reactions
lock[bot]commented, May 8, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IQueryable to Wrap Exceptions - linq
My biggest concern with exposing an IQueryable in my business logic is that it could throw an Entity Framework exception in my business...
Read more >
Exception while using IQueryable Extension "ProjectTo ...
I was using the IQueryable-Extension 'ProjectTo' on some random tests and encountered this issue, but only if the destination type has a ...
Read more >
QueryableExtensions Class (System.Data.Entity)
Returns a new query that will stream the results instead of buffering. This method works by calling the AsStreaming method of the underlying...
Read more >
Mocking IQueryable Extensions with Moq - The Seeley Coder
Mocking IQueryable extensions such as Where or FirstOrDefault on an IQueryable is difficult. Come learn one workaround I found.
Read more >
Avoiding NotSupportedException with IQueryable
Unhandled Exception: System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities. A LINQ ...
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