wpf - 将 SQL 查询转换为 Linq to Entities
问题描述
您好,这是一个 SQL 查询:
select FICHES_ARTICLES.ART_CODE,ART_LIBELLE1,SUM(det_pnet) as PoidsNet
from STOCK_ENT,STOCK_DET,FICHES_ARTICLES,CLIENTS,MVTS_SEQUENCE
where STOCK_ENT.ENT_ID=STOCK_DET.ENT_ID
and STOCK_ENT.ENT_PROP=CLIENTS.CLI_CODE
and STOCK_ENT.ART_CODE=FICHES_ARTICLES.ART_CODE
and STOCK_ENT.ENT_ID=MVTS_SEQUENCE.ENT_ID
and SEQ_STATUT<>'V'
and CLI_CODE='0030000'
group by ART_LIBELLE1,FICHES_ARTICLES.ART_CODE
having SUM(det_pnet)<2000
order by ART_LIBELLE1;
go
我想在 WPF 应用程序中使用 Linq to Entities 编写相同的内容。所以这就是我所拥有的:
private void GetDatas()
{
using(GSUITEEntities dc=new GSUITEEntities())
{
try
{
var query = from ent in dc.STOCK_ENT
join det in dc.STOCK_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
join cli in dc.CLIENTS on ent.ENT_PROP equals cli.CLI_CODE
join seq in dc.MVTS_SEQUENCE on ent.ENT_ID equals seq.ENT_ID
where seq.SEQ_STATUT != "V" && cli.CLI_CODE == "0030000"
group new { ent, det, art, cli, seq } by new
{
art.ART_CODE,
art.ART_LIBELLE1
} into grouped
orderby grouped.Key.ART_LIBELLE1
select new
{
code=grouped.Key.ART_CODE,
lib=grouped.Key.ART_LIBELLE1,
pnet=grouped.Sum(x=>x.det.DET_PNET)
};
}
catch (Exception)
{
throw;
}
}
}
我无法翻译的是Having SUM(det_pnet)
SQL 查询的一部分。有人可以让我朝着正确的方向前进吗?
谢谢
解决方案
尝试使用let 子句:
var query = from ent in dc.STOCK_ENT
join det in dc.STOCK_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
join cli in dc.CLIENTS on ent.ENT_PROP equals cli.CLI_CODE
join seq in dc.MVTS_SEQUENCE on ent.ENT_ID equals seq.ENT_ID
where seq.SEQ_STATUT != "V" && cli.CLI_CODE == "0030000"
group new { ent, det, art, cli, seq } by new
{
art.ART_CODE,
art.ART_LIBELLE1
} into grouped
let sum = grouped.Sum(x => x.det.DET_PNET)
where sum > 2000
orderby grouped.Key.ART_LIBELLE1
select new
{
code = grouped.Key.ART_CODE,
lib = grouped.Key.ART_LIBELLE1,
pnet = sum
};
推荐阅读
- ruby-on-rails - 在rails中订购many_to_many关系
- c - 应用程序未收到 iptable 修改的 Netlink 通知
- ios - 在金属着色器中混合两种纹理
- javascript - 控制台一次只打印一个字母,而不是完整的单词
- c# - SendMessage 总是返回零
- javascript - 为什么android studio中的这个活动打不开?
- php - 存储在数据库中的不良数据
- parameters - 链接两个 Tableau 参数,以便协同工作
- azure-cognitive-search - Azure 搜索错误:对于小数点的 gt 或 ge 运算符,过滤双精度相同
- spring - 如何从 javax.persistence.EntityManger 获取连接字符串