# 請將以下SQL轉換成Linq to sql?

yyy342921318 2010-10-28 09:02:08
sql語句如下，請幫忙轉成linq to sql ,求解~

``````SELECT     *
FROM         COPTC INNER JOIN
COPTD ON COPTC.TC001 = COPTD.TD001 AND COPTC.TC002 = COPTD.TD002 RIGHT OUTER JOIN
MOCTA ON COPTD.TD001 = MOCTA.TA026 AND COPTD.TD002 = MOCTA.TA027 AND
COPTD.TD003 = MOCTA.TA028
WHERE     (MOCTA.TA013 IN ('Y', 'y')) AND (COPTC.TC027 IN ('Y', 'y')) AND (CONVERT(DATETIME, MOCTA.TA003) BETWEEN CONVERT(DATETIME,
'2010-07-20 00:00:00', 102) AND CONVERT(DATETIME, '2010-12-31 00:00:00', 102))
``````
...全文
85 点赞 收藏 6

6 条回复

``````
var query= from cc in COPTC
join cd in COPTD
on new {cc.TC001,cc.TC002} equals new{ cd.TD001,cd.TD002}
into m
from n in m.DefaultIfEmpty()
join ma in MOCTA
on new{cd.TD001, cd.TD002,cd.TD003 } equals new{ ma.TA026,ma.TA027,ma.TA028}

where new char[]{'Y','y'}.Contains(ma.TA013)
&& new char[]{'Y','y'}.Contains(cc.TC027)
&& Convert.ToDateTime(ma.TA003) > Convert.ToDateTime("2010-07-20")
&& Convert.ToDateTime(ma.TA003) < Convert.ToDateTime("2010-12-31")
select new
{
cc,
cd
};
``````

yyy342921318 2010-10-28
[Quote=引用 3 楼 q107770540 的回复:]
C# code

var query= from cc in COPTC
join cd in COPTD
on cc.TC001 equals cd.TD001
&& cc.TC002 equals cd.TD002
into m
from n in ……
[/Quote]

&& cc.TC002 equals cd.TD002

join on的用法怎样带多个呀？谢谢了啊

``````

var query= from cc in COPTC
join cd in COPTD
on cc.TC001 equals cd.TD001
&& cc.TC002 equals cd.TD002
into m
from n in m.DefaultIfEmpty()
join ma in MOCTA
on cd.TD001 equals ma.TA026
&& cd.TD002 equals ma.TA027
&& cd.TD003 equals ma.TA028
where new char[]{'Y','y'}.Contains(ma.TA013)
&& new char[]{'Y','y'}.Contains(cc.TC027)
&& Convert.ToDateTime(ma.TA003) > Convert.ToDateTime("2010-07-20")
&& Convert.ToDateTime(ma.TA003) < Convert.ToDateTime("2010-12-31")
select new
{
cc,
cd
};

``````

``````

var query= from cc in COPTC
join cd in COPTD
on cc.TC001 equals cd.TD001
&& cc.TC002 equals cd.TD002
into m
from n in m
join ma in MOCTA
on cd.TD001 equals ma.TA026
&& cd.TD002 equals ma.TA027
&& cd.TD003 equals ma.TA028
where new char[]{'Y','y'}.Contains(ma.TA013)
&& new char[]{'Y','y'}.Contains(cc.TC027)
&& Convert.ToDateTime(ma.TA003) > Convert.ToDateTime("2010-07-20")
&& Convert.ToDateTime(ma.TA003) < Convert.ToDateTime("2010-12-31")
select new
{
cc,
cd
}
``````

yyy342921318 2010-10-28
[Quote=引用 5 楼 q107770540 的回复:]
C# code

var query= from cc in COPTC
join cd in COPTD
on new {cc.TC001,cc.TC002} equals new{ cd.TD001,cd.TD002}
into m
from n in m.DefaultIfEmpty()
……
[/Quote]

7707

.NET技术 非技术区

2010-10-28 09:02