admin 管理员组

文章数量: 1086019

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.

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)