I need to create objects to return to front end from data I got from database because the response doesnt contain ALL fields of entity and I also add this [NotMapped] propety AmountOfTenants = t.Apartment.Tenants.Count(), to the response.
If I remove ternary operator here t.ApartmentId != null ? and just create new Apartment every time, then, when a Tenant doesnt have any related Apartmetn to him, then my json response contains Tenant with an Apartment object inside of him, where all values are 0/null, thats why i need ternary, to set Apartment to null if Tenant's ApartmentId is null.
Without ternary it works, but i get that apartment with 0/null values and when i add ternary operator i get this error:
System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression'.
Please help
List<Tenant> tenants = await _context.Tenants.Include(tenant => tenant.Apartment.House)
.Select(t => new Tenant
{
Id = t.Id,
FirstName = t.FirstName,
LastName = t.LastName,
PersonalCode = t.PersonalCode,
Birthday = t.Birthday,
PhoneNumber = t.PhoneNumber,
Email = t.Email,
ApartmentId = t.ApartmentId,
Apartment = t.ApartmentId != null ? new Apartment
{
Id = t.Apartment.Id,
Number = t.Apartment.Number,
Floor = t.Apartment.Floor,
AmountOfTenants = t.Apartment.Tenants.Count(),
AmountOfRooms = t.Apartment.AmountOfRooms,
TotalArea = t.Apartment.TotalArea,
AvailableArea = t.Apartment.AvailableArea,
HouseId = t.Apartment.HouseId,
House = t.Apartment.House
} : null
}).ToListAsync();
----------------------------------------------EDIT someone asked for Tenant entity:
namespace RestApi.Models
{
public class Tenant
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PersonalCode { get; set; }
public DateTime Birthday { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public long? ApartmentId { get; set; }
public Apartment? Apartment { get; set; }
}
}
--------------------------EDIT someone asked for Apartment class:
namespace RestApi.Models
{
public class Apartment
{
public long Id { get; set; }
public int Number { get; set; }
public int Floor { get; set; }
public int AmountOfRooms { get; set; }
[NotMapped]
public int AmountOfTenants { get; set; }
public int TotalArea{ get; set; }
public int AvailableArea { get; set; }
public long? HouseId { get; set; }
public House House { get; set; }
public ICollection<Tenant> Tenants { get; set; }
}
}
------------------------------ EDIT I got a friend to check something similar on his machine:
var tenants = await context.Tenants
.Include(t => t.Apartment).ThenInclude(a => a.House)
.Include(t => t.Apartment).ThenInclude(a => a.Tenants)
.Select(t => new Tenant
{
Id = t.Id,
//etc...
Apartment = t.ApartmentId != null ? new Apartment
{
Id = t.Apartment.Id,
NumberOfTenants = t.Apartment.Tenants.Count(),
//etc...
} : null
}).ToListAsync();
on his machine this new statement inside another new statement works and gives no error