【发布时间】:2020-07-11 08:56:37
【问题描述】:
我正在尝试使用以下代码填充 .NET Core 3.1 API 控制器(使用 Entity Framework Core)中类的 int 属性:
[HttpGet("Get")]
public async Task<ActionResult<IEnumerable<Partner>>> Get()
{
List<Device> devices = await _context.Devices.ToListAsync();
List<Partner> items = await (from p in _context.Partners
select new Partner
{
Id = p.Id,
Name = p.Name,
FiscalCode = p.FiscalCode,
Licenses = p.Licenses,
Erp = p.Erp,
ErpName = p.Erp == 1 ? "Erp 1" : "Erp 2",
Devices = devices.Count(k => k.PartnerId == p.Id)
}).OrderBy(k => k.Name).ToListAsync();
return items;
}
Devices = devices.Count(k => k.PartnerId == p.Id) 行生成以下错误:
System.InvalidOperationException: When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression' must return a non-null value of the same type
我做错了什么?
提前致谢
【问题讨论】:
-
@Progman 不,或者至少我没有发现我的问题和那个问题之间有任何相关的联系
-
请edit您的问题将您拥有的源代码包含为minimal reproducible example,其他人可以编译和测试。然后查看meta.stackoverflow.com/questions/333952/… 了解与 SQL 相关的问题。还要解释您要阅读的内容以及
Partner中的Devices属性做什么或应该做什么。 -
设备属性是一个整数。我只想根据标准计算一些项目。没什么花哨的
标签: c# entity-framework-core asp.net-core-3.1 .net-core-3.1