sql-server - 如何将 SQL 命令转换为实体框架和 Lambda 表达式
问题描述
我想将一些 SQL 代码转换为实体框架
select *
into #invoice
from Invoice
where Track = 2
select Fuel.ID, SUM(ISNULL(#invoice.Price, 0)) as Price, SUM(ISNULL(#invoice.Price, 0))
from Fuel left outer join #invoice
on Fuel.ID = #invoice.Fuel
group by Fuel.ID
尝试编写此代码,但结果为零 (0)(价格和升)
var data = fules.GroupJoin(model,
f => f.ID,
m => m.ID,
(f, m) => new { f, m })
.SelectMany(x => x.m.DefaultIfEmpty(),
(x, m) => new { Fuel = x.f, Price = m?.Price, Liter = m?.Liter })
.GroupBy(A => A.Fuel)
.Select(A => new { Fuel = A.Key.Name, Liter = A.Sum(B => B.Liter)
, Price = A.Sum(B => B.Price) })
.ToList();
解决方案
我想你正在寻找这样的东西:
var query = from f in db.Feuls
from i in f.Invoices.Where(x => x.Track == 2).DefaultIfEmpty()
group i by f.ID into g
select new
{
ID = g.Key,
Liter = g.Sum(x => g.Liter),
Price = g.Sum(x => g.Price)
};
推荐阅读
- python - 类型对象“ObservableBase”没有属性“创建”RxPy
- java - 使用 lambda 和流映射列表对象
- java - 如何使用 Java 进行计时
- reactjs - 如何直接在浏览器中打开文件而不用文件名在 React 中下载
- php - 一个 Ajax 调用正在工作,但最后一个没有将数据解析到数据页
- html - 两列中带有空白区域的文本块
- codenameone - Codenamone 项目在模拟器上运行,但不在设备上
- recaptcha - ReCAPTCHA 正在生成内容安全策略警告
- json - 如何使用带有数字范围的 VBA-JSON 解析数据?
- c# - 在 WPF 页面中嵌入 Unity3D 应用程序会导致进程名称在导航到新页面后消失