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

Projekt().To in AutoMapper.QueryableExtensions fails when inner property is null #362

Closed
anders-salomonsen opened this issue Aug 6, 2013 · 5 comments

Comments

@anders-salomonsen
Copy link

When using the unit test ProjectTest2 fails with a null reference exception when setting the Inner Property then it passes. I believe that this is a bug.

using System.Collections.Generic;
using System.Linq;
using AutoMapper.QueryableExtensions;
using Xunit;

namespace AutoMapper.UnitTests
{
public class Test
{
public class DummyData
{
public int Id { get; set; }
public string Navn { get; set; }
public DummyDataInner Inner { get; set; }
}

public class DummyDataDto
{
  public int Id { get; set; }
  public string Navn { get; set; }
  public DummyDataDtoInner Inner { get; set; }
}

public class DummyDataDtoInner
{
  public string Name { get; set; }
}

public class DummyDataInner
{
  public string Name { get; set; }
}

[Fact]
//This test passes as Inner is not null
public void ProjectTest()
{
  Mapper.CreateMap<DummyData, DummyDataDto>();
  Mapper.CreateMap<DummyDataDto, DummyData>();
  Mapper.CreateMap<DummyDataInner, DummyDataDtoInner>();
  Mapper.CreateMap<DummyDataDtoInner, DummyDataInner>();


  var queryableList = new List<DummyData> { new DummyData { Inner = new DummyDataInner() } }.AsQueryable();

  var queryableDtoList = queryableList.Project().To<DummyDataDto>();

  Assert.Equal(1, queryableDtoList.Count());
}

[Fact]
//This test fails as Inner is null
public void ProjectTest2()
{
  Mapper.CreateMap<DummyData, DummyDataDto>();
  Mapper.CreateMap<DummyDataDto, DummyData>();
  Mapper.CreateMap<DummyDataInner, DummyDataDtoInner>();
  Mapper.CreateMap<DummyDataDtoInner, DummyDataInner>();


  var queryableList = new List<DummyData> { new DummyData()}.AsQueryable();

  var queryableDtoList = queryableList.Project().To<DummyDataDto>();

  Assert.Equal(1, queryableDtoList.Count());
}

}
}

@jbogard
Copy link
Member

jbogard commented Aug 6, 2013

Are you seeing this from inside a query provider, like Entity Framework?

On Tue, Aug 6, 2013 at 2:31 AM, anders-salomonsen
notifications@github.comwrote:

When using the unit test ProjectTest2 fails with a null reference
exception when setting the Inner Property then it passes. I believe that
this is a bug.

using System.Collections.Generic;
using System.Linq;
using AutoMapper.QueryableExtensions;
using Xunit;

namespace AutoMapper.UnitTests
{
public class Test
{
public class DummyData
{
public int Id { get; set; }
public string Navn { get; set; }
public DummyDataInner Inner { get; set; }
}

public class DummyDataDto
{
public int Id { get; set; }
public string Navn { get; set; }
public DummyDataDtoInner Inner { get; set; }
}

public class DummyDataDtoInner
{
public string Name { get; set; }
}

public class DummyDataInner
{
public string Name { get; set; }
}

[Fact]
//This test passes as Inner is not null
public void ProjectTest()
{
Mapper.CreateMap<DummyData, DummyDataDto>();
Mapper.CreateMap<DummyDataDto, DummyData>();
Mapper.CreateMap<DummyDataInner, DummyDataDtoInner>();
Mapper.CreateMap<DummyDataDtoInner, DummyDataInner>();

var queryableList = new List { new DummyData { Inner = new DummyDataInner() } }.AsQueryable();

var queryableDtoList = queryableList.Project().To();

Assert.Equal(1, queryableDtoList.Count());
}

[Fact]
//This test fails as Inner is null
public void ProjectTest2()
{
Mapper.CreateMap<DummyData, DummyDataDto>();
Mapper.CreateMap<DummyDataDto, DummyData>();
Mapper.CreateMap<DummyDataInner, DummyDataDtoInner>();
Mapper.CreateMap<DummyDataDtoInner, DummyDataInner>();

var queryableList = new List { new DummyData()}.AsQueryable();

var queryableDtoList = queryableList.Project().To();

Assert.Equal(1, queryableDtoList.Count());
}

}
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/362
.

@anders-salomonsen
Copy link
Author

No just using a generic list:
var queryableList = new List { new DummyData()}.AsQueryable();

Just as the code in the unit test shows.

@jbogard
Copy link
Member

jbogard commented Aug 7, 2013

The idea behind Project().To is to be passed to a query provider like EF or NHibernate that will then do the appropriate SQL creation, not necessarily that the in-memory-execution will work. Project.To has a TON of limitations as it's built explicitly for real query providers, and only does things like MapFrom etc.

To put it another way - don't use Project.To unless you're passing that to EF or NH or another DB query provider that knows what to do with the expression tree.

@anders-salomonsen
Copy link
Author

Ok, I will try that.

Thanks 👍

@lock
Copy link

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.

@lock lock bot locked as resolved and limited conversation to collaborators May 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants