C# “System.Linq.Expressions.ParameterExpression”必须在EF API调用时返回相同类型的非空值

C# “System.Linq.Expressions.ParameterExpression”必须在EF API调用时返回相同类型的非空值,c#,entity-framework-core,asp.net-core-3.1,.net-core-3.1,C#,Entity Framework Core,Asp.net Core 3.1,.net Core 3.1,我正在尝试使用Entity Framework Core用以下代码填充.NET Core 3.1 API控制器中类的int属性: [HttpGet("Get")] public async Task<ActionResult<IEnumerable<Partner>>> Get() { List<Device> devices = await _context.Devices.ToListAsync(); L

我正在尝试使用Entity Framework Core用以下代码填充.NET Core 3.1 API控制器中类的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.Countk=>k.PartnerId==p.Id行生成以下错误: System.InvalidOperationException:从“VisitLambda”调用时,重写“System.Linq.Expressions.ParameterExpression”类型的节点必须返回同一类型的非空值

我做错了什么


提前感谢

您想做的是内部加入他们,最好让EF来处理:

var partners =  from partner in _context.Partners
                join device in _context.Devices on partner.Id equals device.PartnerId into devices
                select new Partner()
                {
                    Id = partner.Id,
                    Name = partner.Name,
                    FiscalCode = partner.FiscalCode,
                    Licenses = partner.Licenses,
                    Erp = partner.Erp,
                    ErpName = partner.Erp == 1 ? "Erp 1" : "Erp 2",
                    Devices = devices
                };

我认为您正在尝试在数据库中进行连接和分组。这样,您只需在数据库中查询一次,所以性能更好

试试这个:

List<Partner> items = await(from p in _context.Partners
                                    join d in _context.Devices
                                        on p.Id equals d.PartnerId
                                    group d by d.Partner /*relation property*/ into g
                                    select new Partner
                                    {
                                        Id = g.Key.Id,
                                        Name= g.Key.Name,
                                        DeviceCount=g.Count()
                                        // select other your properties here
                                    }).OrderBy(k => k.Name).ToListAsync();

这回答了你的问题吗@Progman没有,或者至少我还没有发现我的问题和你的问题之间有任何相关的联系,请你的问题包括你的源代码,它可以被其他人编译和测试。然后,请参阅以了解与SQL相关的问题。还要解释您试图读取的内容以及Partner中的Devices属性的作用或应该是什么。Devices属性是一个整数。我只想根据一个标准计算一些项目。没有什么能指望你的回答,阿米罗辛,但我不想加入,因为那样会增加行数。devices属性是一个整数,表示计数,而不是感谢您的项目列表。这似乎是一条正确的道路,有一些变化。我需要一个左连接而不是内部连接,以及一个生成COUNTd.Id而不是当前计数的语法*
var partners=await _context.Partners.ToListAsync();