4
I am going through the following problem, I am assembling an API for a forum application and this API needs a endpoint returning a collection of Forum
who own the property ParentId
nulla. Then I mounted the following endpoint:
[AllowAnonymous]
[HttpGet]
[Route("toplevel")]
public async Task<IHttpActionResult> GetAllTopLevel(int page = 1, int pageSize = 15)
{
var data = Db.Forums
.Where(f => !f.ParentId.HasValue)
.OrderByDescending(f => f.Name)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(f => new ForumDTO
{
Id = f.Id,
Name = f.Name,
SubForums = f.SubForums.Select(sf => new ForumDTO
{
Id = sf.Id,
Name = sf.Name
}).ToList()
}).ToList();
return Ok(data);
}
But I’m having trouble Select
of this LINQ, which is causing a NotSupportedException
with the following error message:
The type 'Zigforum.Models.Dtos.Forumdto' 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.
Class Forum
:
public class Forum
{
public Forum()
{
SubForums = new List<Forum>();
Posts = new List<Post>();
}
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public virtual Forum Parent { get; set; }
public virtual ICollection<Forum> SubForums { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
Class ForumDTO
:
public class ForumDTO
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("parent_id")]
public int? ParentId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("created")]
public DateTime? Created { get; set; }
[JsonProperty("parent")]
public ForumDTO Parent { get; set; }
[JsonProperty("subforums")]
public List<ForumDTO> SubForums { get; set; }
[JsonProperty("posts")]
public List<PostDTO> Posts { get; set; }
}
I’ve tried some things too, like initializing the property SubForums
with null
, since in the previous error message it says that a type can only be initialized in two places if in both places values are assigned to the same properties and in the same order. To do so seemed to me to compress with the demands:
[AllowAnonymous]
[HttpGet]
[Route("toplevel")]
public async Task<IHttpActionResult> GetAllTopLevel(int page = 1, int pageSize = 15)
{
var data = Db.Forums
.Where(f => !f.ParentId.HasValue)
.OrderByDescending(f => f.Name)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(f => new ForumDTO
{
Id = f.Id,
Name = f.Name,
SubForums = f.SubForums.Select(sf => new ForumDTO
{
Id = sf.Id,
Name = sf.Name,
SubForums = null // Note que fiz a alteração aqui
}).ToList()
}).ToList();
return Ok(data);
}
But it resulted in another mistake:
Unable to create a null Constant value of type 'System.Collections.Generic.List`1[[Zigforum.Models.Viewmodels.Forumdto, Zigforum, Version=1.0.0.0, Culture=neutral, Publickeytoken=null]]'. Only Entity types, enumeration types or Primitive types are supported in this context.
Could someone please help me? I don’t know what else to do.
have tried
SubForums = new List<ForumDTO>()
?– Tobias Mesquita
@Tobymosque also does not work, it is a restriction of LINQ to Entities
– Zignd
usually I use the
AutoMapper
to fill in my DTO/Contracts, posted an answer with an example.– Tobias Mesquita