C# linq 如何编写子查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# linq 如何编写子查询?相关的知识,希望对你有一定的参考价值。

select BillNumber,NameCh,BizTypeCode,CityNameCh,PickUpType,GoodsStateCode,ModeName,TotalCost,OrderQuantity,TotalWeight from
(
select
c.BillNumber as BillNumber,--'账单号't_ds_DeductionBill
b.NameCh as NameCh,--'客户名称't_gt_Customer
a.BizTypeCode as BizTypeCode,--'业务类型'
d.CityNameCh as CityNameCh,--'发件城市't_gt_City
e.PickUpType as PickUpType,--'取件类型't_ds_KdXb_AttachInfo
g.GoodsStateCode as GoodsStateCode,--'是否已取件't_gt_PickupBill
h.ModeName as ModeName,--'国内快递方式't_sc_Internal_ExpChanel
SUM(c.InternalFee)+SUM(c.InternationalFee)as TotalCost,--'总费用't_ds_DeductionBill
COUNT(*) as OrderQuantity,--'订单数量'
TotalWeight=(select SUM(TotalWeight) from t_kd_Order where OrderID in(select OrderID from t_ds_DeductionBill where BillNumber=c.BillNumber))
from t_kd_Order AS a
TotalWeight 这个部分子查询在C# linq中如何写?

string[] ids = (from DeductionBill in t_ds_DeductionBill where t_ds_DeductionBill .BillNumber.Equals(c.BillNumber)) .ToArray<string>();

TotalWeight= (from Order in t_kd_Order where(item => ids.Contains<string>(item.OrderID))
select Order.TotalWeight ).Sum();
参考技术A public List<JMIntegral.DBUtility.BBCustomer> GetList(string name, string product)

var table = from c in _db.BBCustomer
where (!string.IsNullOrEmpty(name) ? c.Name.Contains(name) : true) &&
(!string.IsNullOrEmpty(product) ? c.ProductName.Contains(product) : true)
orderby c.CID descending
select c;
return table.ToList();

LINQ 选择中的 C# 子查询

【中文标题】LINQ 选择中的 C# 子查询【英文标题】:C# subquery in LINQ select 【发布时间】:2021-12-07 17:18:23 【问题描述】:

我正在尝试将此查询转换为 LINQ,但我没有得到子查询的值

查询:

select c.cod, c.cpfcnpj, c.razaosocial, c.nome, c.fone, c.celular, c.email, c.dtcad, s.dataval as validade,
(select max(datapagamento) from vendas where c.cod = coduser) as datapag
from usuarios c, libsys s
WHERE c.cod = s.codcli
and c.cod in (select coduser from vendas)
AND c.cod in (select l.codcli from libsys l where l.dataval >= current_date)
order by c.dtcad asc

LINQ:

        var rel = await (from u in _contexto.usuarios
                         from v in _contexto.libsys
                         where (
                                 (u.cod == v.codcli) &&
                                 _contexto.vendas.Any(y => y.coduser == u.cod) &&
                                 _contexto.libsys.Any(y => y.codcli == u.cod && y.pcpdataval >= System.DateTime.Now)
                               )
                         select new RelatorioLicsModel
                         
                             
                             cod = u.cod,
                             cpfcnpj = u.cpfcnpj,
                             razaosocial = u.razaosocial,
                             nome = u.nome,
                             fone = u.fone,
                             celular = u.celular,
                             email = u.email,
                             dtcad = u.dtcad,
                             validade = v.pcpdataval.ToString(),
                             dtpag = Convert.ToDateTime(_contexto.vendas.Where(s => s.datapagamento == _contexto.vendas.Max(x => x.datapagamento) && s.coduser == u.cod).FirstOrDefault())
                         ).ToListAsync();

我得到的错误是:

“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '(PARTITION BY v.coduser ORDER BY v 附近使用的正确语法.cod) AS row\r\n FROM vendas ' 在第 7 行"

这是正确的方法吗?感谢您的帮助!

【问题讨论】:

【参考方案1】:

dtpag 属性存在问题。

var query = 
    from u in _contexto.usuarios
    from v in _contexto.libsys
    where (
            (u.cod == v.codcli) &&
            _contexto.vendas.Any(y => y.coduser == u.cod) &&
            _contexto.libsys.Any(y => y.codcli == u.cod && y.pcpdataval >= System.DateTime.Now)
        )
    select new RelatorioLicsModel
    
        
        cod = u.cod,
        cpfcnpj = u.cpfcnpj,
        razaosocial = u.razaosocial,
        nome = u.nome,
        fone = u.fone,
        celular = u.celular,
        email = u.email,
        dtcad = u.dtcad,
        validade = v.pcpdataval.ToString(),
        dtpag = Convert.ToDateTime(_contexto.vendas.Where(s => s.coduser == u.cod).Max(x => x.datapagamento))
    

【讨论】:

以上是关于C# linq 如何编写子查询?的主要内容,如果未能解决你的问题,请参考以下文章

Linq 到实体子查询

如何编写异步LINQ查询?

如何在linq C#中使用group by并获取记录列表[重复]

如何使用实体框架和 linq 编写此 sql 查询

如何编写此 LINQ 查询的 SQL 版本?

如何在 Linq 中编写嵌套的 Sql 查询 [关闭]