简体   繁体   English

EF核心:IGrouping中的异步嵌套选择导致ArgumentException

[英]EF Core: Async nested select within IGrouping causing ArgumentException

Updated with other relevant classes 与其他相关课程一起更新

public class TrendItem
{
    public string ItemTitle { get; set; }

    public IEnumerable<string> ItemValues { get; set; }
}

public class TrendValue
{
    public int Id { get; set; }

    public TrendResultType TrendType { get; set; }

    public string Value { get; set; }

    public Trend Trend { get; set; } // contains DateRecorded property
}

Please see the function below that leverages on EF Core (2.1): 请参阅以下利用EF Core(2.1)的功能:

    public async Task<List<TrendItem>> GetTrends(
        int companyId,
        TrendResultType type,
        DateTimeOffset startDate,
        DateTimeOffset endDate,
        RatingResultGroup group
    )
    {
        var data = _dataContext.TrendValues.Where(rr =>
                        rr.Trend.CompanyId == companyId &&
                        rr.TrendType == type &&
                        rr.Trend.DateRecorded >= startDate &&
                        rr.Trend.DateRecorded <= endDate);

        return await data.GroupBy(rr => new { rr.Trend.DateRecorded.Year, rr.Trend.DateRecorded.Month })
                         .Select(g => new TrendItem() { ItemTitle = $"{g.Key.Year}-{g.Key.Month}", ItemValues = g.Select(rr => rr.Value) })
                         .ToListAsync();
    }

I'm getting problems, specifically with the portion g.Select(rr => rr.Value) , where I intended to select a collection of values (strings). 我遇到问题,特别是g.Select(rr => rr.Value)部分 ,我打算在其中选择值(字符串)的集合。

Whenever I try to change that to something else like g.Sum(rr => int.Parse(rr.Value)) , it works fine. 每当我尝试将其更改为g.Sum(rr => int.Parse(rr.Value))之类的东西时 ,它都可以正常工作。 It's the retrieval of the collection that seems to be a problem. 这似乎是一个问题的集合的检索。

I always get ArgumentException: Argument types do not match . 我总是得到ArgumentException:参数类型不匹配

Is this due to the async function? 这是由于异步功能引起的吗?

Hmm, I'd start by looking to simplify the data retrieval into an anonymous type to perform the grouping, then transform into the view model. 嗯,我首先希望将数据检索简化为匿名类型以执行分组,然后转换为视图模型。 It may be that EF is getting a bit confused working with the grouped items values. EF与分组项目值的使用可能有些混淆。

It looks like you want to group by date (year-month) then have a list of the values in each group: 您似乎想按日期(年-月)分组,然后在每个分组中都有一个值列表:

var query = _dataContext.TrendValues
              .Where(rr =>
                rr.Trend.CompanyId == companyId 
                && rr.TrendType == type 
                && rr.Trend.DateRecorded >= startDate 
                && rr.Trend.DateRecorded <= endDate)
              .Select(rr => new 
              { 
                TrendYear = rr.Trend.DateRecorded.Year,
                TrendMonth = rr.Trend.DateRecorded.Month,
                rr.Value
              }).ToListAsync();

var data = query.GroupBy(rr => new { rr.TrendYear, rr.TrendMonth })
            .Select(g => new TrendItem() { ItemTitle = $"{g.Key.Year}-{g.Key.Month}", ItemValues = g.ToList().Select(rr => rr.Value).ToList() })
            .ToList();
return data;

which could be simplified to: 可以简化为:

var query = _dataContext.TrendValues
             .Where(rr =>
               rr.Trend.CompanyId == companyId &&
               rr.TrendType == type &&
               rr.Trend.DateRecorded >= startDate &&
               rr.Trend.DateRecorded <= endDate)
               .Select(rr => new 
               { 
                 TrendDate = rr.Trend.DateRecorded.Year + "-" + rr.Trend.DateRecorded.Month,
                 rr.Value
               }).ToListAsync();

var data = query.GroupBy(rr => rr.TrendDate)
             .Select(g => new TrendItem() { ItemTitle = $"{g.Key}", ItemValues = g.ToList().Select(rr => rr.Value).ToList() })
             .ToList();
return data;

The selecting of the TrendDate in the query may need some work if Trend.Year and Trend.Month are numeric fields. 如果Trend.Year和Trend.Month是数字字段,则在查询中选择TrendDate可能需要做一些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM