带有子查询和左联接的LINQ

时间:2018-07-19 02:20:18

标签: linq subquery ef-core-2.1

我正在尝试编写LINQ查询以生成下面的SQL查询。我知道它看起来像是赋值,但是尝试了一些语法错误的查询。

select pm.Profile_Number,PD.Line_Abbrev,PD.Group_Code from Profile_Detail PD
INNER JOIN  Profile_Master PM ON pd.profile_id = pm.profile_id
LEFT JOIN
(
    SELECT Field_Abbr,Group_Code FROM vw_Group_Code
    where ((US=1 AND Group_US_Obsolete <> 1) OR (CA=1 AND Group_CA_Obsolete <> 1) OR (MX=1 AND Group_MX_Obsolete <> 1))

)gcv ON PD.Line_Abbrev = gcv.Field_Abbr AND PD.Group_Code = gcv.Group_Code
WHERE PD.Profile_Id IN(42) AND gcv.Field_Abbr IS NULL

试用版:

(from pd in _context.ProfileDetail
                                                           join pm in _context.ProfileMaster on pd.ProfileId equals pm.ProfileId
                                                           join vla in _context.VwLineAbbrvs on pd.LineAbbrev equals vla.LineAbbrev into gc
                                                           from vla in gc.DefaultIfEmpty()
                                                           where profileNumber.Contains(pd.ProfileId.ToString()) &&
                                                           ((vla.IsUS && vla.USObsolete) || (vla.IsCA && !vla.CAObsolete) || (vla.IsMX && !vla.MXObsolete))
                                                           select new ObsoleteLineDetail
                                                           {
                                                               LineAbbrv = pd.LineAbbrev,
                                                               GroupCode = pd.GroupCode,
                                                               ProfileNumber = pm.ProfileNumber
                                                           }).ToList();

0 个答案:

没有答案