ASP.NET CORE EF - LINQ语句

时间:2017-09-10 00:37:04

标签: linq asp.net-core entity-framework-core

我有一个SQL语句来从数据库中获取一些数据。

select *, (SELECT count(ReagentID) FROM dbo.ReagentWarehouse WHERE dbo.Reagent.ReagentID = dbo.ReagentWarehouse.ReagentID) as InStock
from dbo.Reagent
join dbo.Supplier on dbo.Supplier.SupplierID = dbo.Reagent.SupplierID
join dbo.Unit on dbo.Unit.UnitID = dbo.Reagent.UnitID

SQL语句可以正常工作。

我试图将其转换为LINQ EF Core。

我已准备好以下声明:

var viewModelReagents_W_Stock = from i in _context.Reagents.Include(i => i.Supplier).Include(i => i.Unit).Include(i => i.Supplier)
                              select new ReagentWStock
                              {
                                ReagentID = i.ReagentID,
                                UnitID = i.UnitID,
                                SupplierID = i.SupplierID,
                                ReagentName = i.ReagentName,
                                Supplier = i.Supplier,

                                InStock = (from p in _context.ReagentWarehouses
                                           where p.ReagentID.Equals(i.ReagentID)
                                           select p).Count()
                            };

这会产生错误:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
  An unhandled exception has occurred while executing the request
System.InvalidOperationException: No value provided for required parameter '_outer_ReagentID'.

从控制台LOG这个LINQ是:

              SELECT [i.Supplier].[SupplierID], [i.Supplier].[Name], [i].[ReagentID], [i].[UnitID], [i].[SupplierID], [i].[ReagentShortcut], [i].[ReagentName], [i].[Number], [i.Unit].[UnitShortcut], (
                  SELECT COUNT(*)
                  FROM [ReagentWarehouse] AS [p]
                  WHERE [p].[ReagentID] = @_outer_ReagentID
              )
              FROM [Reagent] AS [i]
              INNER JOIN [Supplier] AS [i.Supplier] ON [i].[SupplierID] = [i.Supplier].[SupplierID]
              INNER JOIN [Unit] AS [i.Unit] ON [i].[UnitID] = [i.Unit].[UnitID]
              ORDER BY [i].[ReagentName]
              OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY

型号:

public class Reagent
{
    public int ReagentID { get; set; }
    public int UnitID { get; set; }
    public int SupplierID { get; set; }
    public string ReagentName { get; set; }
    public Unit Unit { get; set; }
    public Supplier Supplier { get; set; }
    public ICollection<ReagentWarehouse> Warehouses { get; set; }
}

public class ReagentWarehouse
{
    public int ReagentWarehouseID { get; set; }
    public int LocationID { get; set; }
    public int ReagentID { get; set; }
    public int Number { get; set; }
    public DateTime DeliveryDate { get; set; }
    public decimal Quantity { get; set; }
    public Reagent Reagent { get; set; }
 }

由于

1 个答案:

答案 0 :(得分:0)

首先你可以简化linq查询(包含不需要和InStock 更简单):

var viewModelReagents_W_Stock = from i in context.Reagents
                                select new ReagentWStock
                                {
                                    ReagentID = i.ReagentID,
                                    UnitID = i.UnitID,
                                    SupplierID = i.SupplierID,
                                    ReagentShortcut = i.ReagentShortcut,
                                    ReagentName = i.ReagentName,
                                    Number = i.Number,
                                    Supplier = i.Supplier,
                                    UnitShortcut = i.Unit.UnitShortcut,
                                    InStock = i.Warehouses.Count()
                                };

其次,您可能需要更多代码配置,因为您的问题无法再现。 _outer_ReagentID 来自哪里? 我得到的生成的Sql(并且它可以工作):

SELECT [i.Supplier].[SupplierID], [i.Supplier].[Name], [i].[ReagentID], [i].[UnitID], [i].[SupplierID] AS [SupplierID0], [i].[ReagentShortcut], [i].[ReagentName], [i].[Number], [i.Unit].[UnitShortcut], (
    SELECT COUNT(*)
    FROM [ReagentWarehouse] AS [p]
    WHERE [p].[ReagentID] = [i].[ReagentID]
) AS [InStock]
FROM [Reagent] AS [i]
INNER JOIN [Unit] AS [i.Unit] ON [i].[UnitID] = [i.Unit].[UnitID]
INNER JOIN [Supplier] AS [i.Supplier] ON [i].[SupplierID] = [i.Supplie.[SupplierID]
ORDER BY [i].[ReagentName]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY
@__p_0 int, @__p_1 int, @__p_0=10,@__p_1=10

第三次使用较新版本的NuGet库,至少1.1.2,但最好切换到NetCore 2.0和EFCore 2.0