I'm going through the following problem, I am putting an API into a forum application and this API needs an endpoint to return a collection of Forum
that have the% property of% null. Then I put 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 problems with ParentId
of this LINQ, which is causing a Select
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 NotSupportedException
:
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 Forum
:
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 have tried a few things too, such as initializing the ForumDTO
property with SubForums
, since in the previous error message it says that a type can only be initialized in two places if both places are assigned values to same properties and in the same order. Doing this seemed to compress me 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 error:
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 anyone help me please? I do not know what else to do.