我在警报和匹配表之间有父子一对多关系,这些表之间的分组可以与聚合函数计数、最大值等一起正常工作。但是当我尝试选择匹配记录以将其显示在逗号分隔的字符串中时,它会给出以下错误。
System.InvalidOperationException: The LINQ expression 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
这是我的基本查询
join m in _clientContext.AlertsMatches
on a.AlertID equals m.alertID
group new { a, m } by new
{
a.AlertID,
a.AlertDate,
a.AlertScore,
} into gr
select new
{
AlertId = gr.Key.AlertID,
AlertDate = gr.Key.AlertDate,
AlertScore = gr.Key.AlertScore,
ScenarioNames = gr.Select(x => x.m.Scenario.ScenarioName),
};
var query = from item in groupedQuery
select new AlertsDTO
{
AlertId = item.AlertId,
AlertDate = item.AlertDate,
Scenario = string.Join(", ", item.ScenarioNames)
};```
我首先想到的是你可以明确地填充你的场景。
Parallel.Foreach(query, item => {
item.Scenario = string.Join(", ", item.ScenarioNames);
});
其他:
我认为这是关于未执行 IQueryable/IEnumerable 场景的错误,在重新选择它之前思考,因为数据尚不存在,但它只是查询。每次选择后,您可以对它们使用 tolist 。我对此不确定,仅供参考。