Linq加入存在

时间:2012-08-15 06:02:41

标签: linq join contains

我正在忙于重写系统并使用Linq查询从数据库中提取数据。我习惯于简单的旧TSQL和存储过程,所以我的Linq技能不是最好的。 我有一个SQL查询,我尝试在Linq中重写包含join,where子句和IN语句。我确实做对了但是当我运行sql查询时,我从Linq查询获得了不同的值。在某个地方,我遗漏了一些东西而无法找到原因。

这是SQL:

select 
    isnull(Sum(QtyCC) + Sum(QtyEmployee), 0) *
    isnull(Sum(UnitPrice), 0)[TotalRValue]             
from 
    tbl_app_KGCWIssueLines a
        inner join tbl_app_KGCWIssue b on b.IssueNrLnk = a.IssueNrLnk
where   
    b.CreationDate >= '2011-02-01' and 
    a.IssueNrLnk IN (
        select 
            IssueNrLnk
        from 
            tbl_app_KGCWIssue
        where   
            CustomerCode = 'PRO002' and   
            ISNULL(Tier1,'') = 'PRO002' and   
            ISNULL(Tier2,'') = 'HAMD01' and   
            ISNULL(Tier3,'') = '02' and   
            ISNULL(Tier4,'') = '02001' and  
            ISNULL(Tier5,'') = 'PTAHQ001' and   
            ISNULL(Tier6,'') = '035' and   
            ISNULL(Tier7,'') = '' and   
            ISNULL(Tier8,'') = '' and   
            ISNULL(Tier9,'') = '' and   
            ISNULL(Tier10,'') = ''
)

这是Linq:

ctx.ObjectContext.tbl_app_KGCWIssue
    .Join(ctx.ObjectContext.tbl_app_KGCWIssueLines, 
        i => i.IssueNrLnk, l => l.IssueNrLnk, (i, l) => new { i, l })
    .Where(o => o.i.CreationDate >= IntervalStartDate)
    .Where(p => ctx.ObjectContext.tbl_app_KGCWIssue
        .Where(a => 
            a.CustomerCode == CustomerCode && 
            a.Tier1 == employee.Tier1 && 
            a.Tier2 == employee.Tier2 && 
            a.Tier3 == employee.Tier3 && 
            a.Tier4 == employee.Tier4 && 
            a.Tier5 == employee.Tier5 && 
            a.Tier6 == employee.Tier6 && 
            a.Tier7 == employee.Tier7 && 
            a.Tier8 == employee.Tier8 && 
            a.Tier9 == employee.Tier9 && 
            a.Tier10 == employee.Tier10)
        .Select(i => i.IssueNrLnk)
        .Contains(p.l.IssueNrLnk))
    .Sum(p => p.l.UnitPrice * (p.l.QtyEmployee + p.l.QtyCC));

0 个答案:

没有答案