最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - GroupBy problem in .NET 3.1 .First()' could not be translated. Either rewrite the query in a form that can be trans

programmeradmin5浏览0评论

Models look like this, I made them shorter for explanation


public partial class Child : Entity<int>, IAggregateRoot
{

    public Lrp Lrp { get; set; }
    public int LrpId { get; set; }
    public Country Country { get; set; }
    public int CountryId { get; set; }
     public Product Product { get; set; }
     public int? ProductId { get; set; }
}

public class Product {
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Lrp {
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Product {
    public int Id { get; set; }
    public string Name { get; set; }
}

My returnig Dto look like this

public class LrpReportDTO
{
    public int Id { get; set; }
    public FunderDto Funder { get; set;
    public ProductDto Product { get; se
    public LrpDto Lrp { get; set; }
    public EnumValueDto Status { get; s
    public int Count { get; set; }
}
public class FunderDto
{
    public int? Id { get; set; }
    public string Name { get; set; }
}
public class ProductDto
{
    public int? Id { get; set; }
    public string Name { get; set; }
}
public class LrpDto
{
    public int?   Id   { get; set; }
    public string Name { get; set; }
}
public class EnumValueDto
{
    public string Value { get; set; }
    public string Name { get; set; }
}

I want to groupd them and get needed data

public async Task<List<LrpReportDTO>> GetGroupedLrpReportData(Expression<Func<Child, bool>> filter, CancellationToken cancellationToken)
{
    return await DbContext.Set<Child>()
        .Where(filter)
        .GroupBy(c => new { c.LrpId, c.CountryId, c.FunderId })
        .Select(group => new LrpReportDTO
        {
            Id = group.Key.LrpId,
            Funder = new FunderDto
            {
                Id = group.Key.FunderId,
                Name = group.First().Funder.Name
            },
            Product = new ProductDto
            {
                Id = group.First().Product.Id,
                Name = group.First().Product.Name
            },
            Lrp = new LrpDto
            {
                Id = group.Key.LrpId,
                Name = group.First().Lrp.Name
            },
            Status = group.First().Status != null
                ? new EnumValueDto
                {
                    Value = group.First().Status.ToString(),
                    Name = group.First().Status.ToString()
                }
                : null,
            Count = group.Sum(x => x.ChildSupporterLinks.Count)
        })
        .ToListAsync(cancellationToken);
}

Filter looks like this, there can be more conditiotions

Expression<Func<Child, bool>> filter = c => c.ChildSupporterLinks.Any(link => link.DateCreated <= dateForFilter && link.Active);

I want to group this entites by 3 field and then convert it to the Dto, But, I'm getting an exception

I get this kind of exception

The LINQ expression '(GroupByShaperExpression:KeySelector: new {LrpId = (c.LrpId),CountryId = (c.CountryId),FunderId = (c.FunderId)},ElementSelector:(EntityShaperExpression:EntityType: ChildValueBufferExpression:(ProjectionBindingExpression: EmptyProjectionMember)IsNullable: False)).First()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See /?linkid=2101038 for more information.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论