繁体   English   中英

SQL 到 Linq C# EntityFrameWork 查询

[英]SQL to Linq C# EntityFrameWork Query

我无法在 EntityFrameWork 上编写此查询。 这很简单,但我还不知道 EF。* 我需要 EF 上的 4 个表左连接。

select ProductId,Name,ValueName,Value from ProductTechnicalInfo p 
left join ProductTechnicalInfoParameter pp on pp.ProductTechnicalInfoId = p.Id 
left join ProductTechnicalInfoValue v on v.ProductTechicalInfoId = p.Id
left join ProductTechnicalInfoValueParameter vp on vp.TechnicalValuesId=v.Id
where ProductId = 22

我的 EF 代码:

var infos = infoProductsRep.Join(transaction.ProductTechnicalInfoParameter, p => p.Id, pp => pp.ProductTechnicalInfoId, (ProductTechnicalInfo, ProductTechnicalInfoParameter) => new
{
    ProductTechnicalInfo,
    ProductTechnicalInfoParameter
}).Join(transaction.ProductTechnicalInfoValue, p => p.ProductTechnicalInfo.Id, v => v.Id, (Info, Value) => new
{
    Info,
    Value
}).Join(transaction.ProductTechnicalInfoValueParameter, p => p.Value.Id, vp => vp.TechnicalValuesId, (Info, Result) => new
{
    Info,
    Result
}).Where(x => x.Info.Info.ProductTechnicalInfo.ProductId == product.Id).ToList();

属性名称可能存在一些问题,但您的查询很容易转换为 LINQ:

var query = 
    from p in infoProductsRep
    join pp in transaction.ProductTechnicalInfoParameter on p.Id equals pp.ProductTechnicalInfoId into gpp
    from pp in gpp.DefaultIfEmpty()
    join v in transaction.ProductTechnicalInfoValue on p.Id equals v.ProductTechicalInfoId into gv
    from v in gv.DefaultIfEmpty()
    join vp in transaction.ProductTechnicalInfoValueParameter on p.Id equals vp.TechnicalValuesId into gvp
    from vp in gvp.DefaultIfEmpty()
    where p.ProductId == 22
    select new 
    {
        p.ProductId,
        pp.Name,
        v.ValueName,
        vp.Value
    };

var result = query.ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM