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 use [MapDerivedType] with Map(IQueryable)

See original GitHub issue

Describe the bug When using [MapDerivedType] and Map(IQuerable) in the same class, the generated code includes a switch statement in the Queryable.Select lambda

To Reproduce Steps to reproduce the behavior:

  1. Declare a type Hierarchy with at least one base class and one derived class - let’s call them Parent and Child
  2. Declare corresponding DTO types for those classes - ParentDto and ChildDto
  3. Declare a static Mapper class and add a method with the following Signature : [MapDerivedType<Child, ChildDto>] public static partial ParentDto ToDto(Parent model);
  4. Add the following method to the Mapper: public partial static IQueryable<ParentDto> ProjectToDto(this IQueryable<Parent> query);
  5. The generated code will contain a switch statement in the generated lambda, which is not valid for Queryable expressions

Expected behavior The generated code should compile, and ideally map the hierachy correctly

Code snippets

public abstract class Parent {}
public class Child : Parent {}

public abstract class ParentDto {}
public class ChildDto: ParentDto {} //the inheritance here is unimportant, but is relevant for my use case

[Mapper]
public static partial class ParentChildMapper
{
    [MappDerivedType<Child, ChildDto>()]
    public static partial ParentDto ToDto(this Parent parent);

    public static partial IQuerable<ParentDto> ProjectToDto(this IQueryable<Parent> query);
}

Environment (please complete the following information):

  • Mapperly Version: main branch (unreleased)
  • .NET Version: 7.0.203
  • Target Framework: .net7.0
  • IDE: Rider 2023.1.1
  • OS:

Additional context

I know this is pre-release, and I’m not using it in production, but it would be a nice use-case to be fulfilled.

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
x2764techcommented, May 6, 2023

@onionhammer It works - EF core will select the required columns and then do the “is” calculation client side. It may lead to over fetching, and maybe left joins (which might be avoided using subqueries with EF7)

1reaction
latonzcommented, May 8, 2023

This should now be fixed on main.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Is it possible to map IQueryable<CatDTO ...
I believe the functionality you want is in UseAsDataSource. You can't map IQueryable, but you shouldn't need to with UseAsDataSource Example
Read more >
IQueryable no supported?
Hello, Isn't possible to map an IQueriable with AutoMapper? Articles = Mapper.Map<IQueryable<Article>, IQueryable<ArticleItemViewModel>>(articleRepository.
Read more >
Work with IQueryables in AutoMapper #3779
Hi, consider that I have a query which return IQueryable<Company> and I should map it to IQueryable<CompanyDto> . When I use the AutoMapper, ......
Read more >
Queryable Extensions
Map functions, you may notice that the ORM will query all the fields of all the objects within a graph when AutoMapper is...
Read more >
r/dotnet - Mapperly - A .NET source generator for object to ...
This one looks really interesting as the source generator is the focus and not added to a mapper that was previously written in...
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