我如何在 linq c#(EF Core 6) 中撰寫查詢總價,并將 DTO 的其他欄位與總價一起映射。
sql 查詢:
′′′ SELECT (sum(c.ExtraPrice) (a.PricePerSqM*10)) as TotalPrice FROM dbo.Cities a JOIN dbo.CityExtras b ON a.CityId = b.CityId JOIN dbo.Extras c ON b .ExtrasId = c.ExtrasId 其中 a.CityId = 1 按 PricePerSqM′′′ 分組
My Models:
′′′ public class City
{
public int CityId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int PricePerSqM { get; set; }
public ICollection<CityExtras> CityExtras { get; set; }
}
public class Extras
{
public int ExtrasId { get; set; }
[Required]
public string ExtrasName { get; set; }
public ICollection<CityExtras> CityExtras { get; set; }
}
public class CityExtras
{
public int CityId { get; set; }
public City City { get; set; }
public int ExtrasId { get; set; }
public Extras Extras { get; set; }
public int ExtraPrice { get; set; }
public int TotalPrice { get; set; }
}
public class QuotationDTO
{
public string City { get; set; }
public int PricePerSqM { get; set; }
public List<string>? Extras { get; set; }
public List<int>? Price { get; set; }
public int Totalprice { get; set; }
}′′′
uj5u.com熱心網友回復:
嘗試以下查詢:
var query =
from a in ctx.Cities
from b in a.CityExtras
where a.CityId == 1
group new { a, b } by new { a.PricePerSqM } into g
select new
{
g.Key.PricePerSqM,
TotalPrice = g.Sum(x => x.b.ExtraPrice) g.Key.PricePerSqM * 10
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517673.html
標籤:C#sql服务器林克.net-coresql 到 linq 的转换