将具有一对多关系的 SQL 查询转换为具有 parent/children 对象结构的 ASP.NET 核心/Entity Framework 核心调用
Convert SQL query with a one-to-many relationship to an ASP.NET Core / Entity Framework Core call with a parent/children object structure
你可以在 SqlFiddle 上看到我的数据库 table 结构:
http://sqlfiddle.com/#!18/05fce/12/0
我有这个 SQL 查询 returns 产品列表 (webspiderproduct table) 并且对于每个产品它列出了每个商店的最低价格 (webspiderproductprice table).这是查询:
select
t1.ProductId,
t1.productname,
t2.shopId,
t2.price as lowestPrice
from
webspiderproduct t1
join
(select
*,
row_number() over (partition by webspiderproductid, shopid
order by price asc) as rn
from webspiderproductprice) t2 on t1.productid = t2.webspiderproductid
where
rn = 1
order by
ProductId
我现在正在尝试转换此查询,以便可以使用 Entity Framework 核心通过 ASP.NET 核心调用它。问题是我宁愿它 returns 它的数据是这样的:
ProductId: 1, ProductName: "sample product1"
shopId: 1, lowestPrice: 100
shopId: 2, lowestPrice: 199
ProductId: 2, ProductName: "sample product 2"
shopId: 1, lowestPrice: 99
shopId: 2, lowestPrice: 119
shopId: 3, lowestPrice: 19
以便可以使用外部 foreach 产品循环轻松地在视图中循环,然后为每个 product.lowestShopPrices 循环调用内部循环。
但是查询的结构应该是 returns 一个产品列表,其中每个产品包含一个 shopids/shopprices.
列表
如何更改 SQL 查询,使其 returns 使用 Entity Framework 核心和 parent/children 结构的数据?
假设您的课程如下所示:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ProductPrice
{
public int Id { get; set; }
public int ShopId { get; set; }
public int Price { get; set; }
public int ProductId { get; set; }
}
您可以编写 EF 查询:
var result = ctx.Products
.Join(ctx.Prices
.GroupBy(n => new { n.ProductId, n.ShopId })
.Select(n => new{ n.Key.ProductId, n.Key.ShopId, MinPrice = n.Min(m => m.Price) }),
p => p.Id, pp => pp.ProductId, (p, pp) => new { p.Id, p.Name, pp.ShopId, pp.MinPrice })
.OrderBy(n => n.Id)
.ToList();
这将产生您需要的结果。
你可以在 SqlFiddle 上看到我的数据库 table 结构:
http://sqlfiddle.com/#!18/05fce/12/0
我有这个 SQL 查询 returns 产品列表 (webspiderproduct table) 并且对于每个产品它列出了每个商店的最低价格 (webspiderproductprice table).这是查询:
select
t1.ProductId,
t1.productname,
t2.shopId,
t2.price as lowestPrice
from
webspiderproduct t1
join
(select
*,
row_number() over (partition by webspiderproductid, shopid
order by price asc) as rn
from webspiderproductprice) t2 on t1.productid = t2.webspiderproductid
where
rn = 1
order by
ProductId
我现在正在尝试转换此查询,以便可以使用 Entity Framework 核心通过 ASP.NET 核心调用它。问题是我宁愿它 returns 它的数据是这样的:
ProductId: 1, ProductName: "sample product1"
shopId: 1, lowestPrice: 100
shopId: 2, lowestPrice: 199
ProductId: 2, ProductName: "sample product 2"
shopId: 1, lowestPrice: 99
shopId: 2, lowestPrice: 119
shopId: 3, lowestPrice: 19
以便可以使用外部 foreach 产品循环轻松地在视图中循环,然后为每个 product.lowestShopPrices 循环调用内部循环。
但是查询的结构应该是 returns 一个产品列表,其中每个产品包含一个 shopids/shopprices.
列表如何更改 SQL 查询,使其 returns 使用 Entity Framework 核心和 parent/children 结构的数据?
假设您的课程如下所示:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ProductPrice
{
public int Id { get; set; }
public int ShopId { get; set; }
public int Price { get; set; }
public int ProductId { get; set; }
}
您可以编写 EF 查询:
var result = ctx.Products
.Join(ctx.Prices
.GroupBy(n => new { n.ProductId, n.ShopId })
.Select(n => new{ n.Key.ProductId, n.Key.ShopId, MinPrice = n.Min(m => m.Price) }),
p => p.Id, pp => pp.ProductId, (p, pp) => new { p.Id, p.Name, pp.ShopId, pp.MinPrice })
.OrderBy(n => n.Id)
.ToList();
这将产生您需要的结果。