Я использую asp.net aspnetcoreodata с минимальным api. DTOS: < /p>
Код: Выделить всё
public class Customer
{
public int Id { get; set; }
public string FullName { get; set; }
public DateTime DateOfBirth { get; set; }
public ICollection CustomerRatings { get; set; }
}
public class CustomerRating
{
public DateTime CreatedAt { get; set; }
public RatingType RatingType { get; set; }
}
public class CustomerSummaryDto
{
public int Id { get; set; }
public string FullName { get; set; }
public DateTime DateOfBirth { get; set; }
public CustomerRatingDto LatestRating { get; set; }
}
public class CustomerRatingDto
{
public RatingType RatingType { get; set; }
}
public enum RatingType
{
Low,
Medium,
High
}
< /code>
Мой запрос выглядит следующим образом: < /p>
var queryProjection = dbContext.Customers.Select(c => new CustomerSummaryDto
{
Id = c.Id,
FullName = c.FullName,
DateOfBirth = c.DateOfBirth,
LatestRating = c.CustomerRatings
.OrderByDescending(r => r.CreatedAt)
.Select(r => new CustomerRatingDto { RatingType = r.RatingType })
.FirstOrDefault()
});
var finalQuery = (IQueryable)queryOptions.ApplyTo(queryProjection, new ODataQuerySettings());
var result = await finalQuery.ToListAsync();
< /code>
Я хочу позволить клиенту делать: < /p>
GET /odata/CustomerSummaryDtos?$orderby=LatestRating/RatingType desc
< /code>
Но я получаю следующую ошибку: < /p>
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
"title": "An error occurred while processing your request.",
"status": 500,
"detail": "Cannot apply ODataQueryOptions of 'MyApp.Dto.CustomerSummaryDto' to IQueryable of 'MyApp.Models.Customer'. (Parameter 'query')"
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... jected-dto
Мобильная версия