select * FROM table1
JOIN table2
ON table1.MzRegId = table2.ID
JOIN table3
ON ( table1.MzRegId = table3.MzRegId
OR table2.CardNo = table3.CardNo )
怎么把上面的sql 语句转换为 linq
7 个解决方案
#1
本帖最后由 caozhy 于 2014-10-10 11:27:40 编辑
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
#2
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
#3
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
#4
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
用Union查询太复杂了,我贴出来的只是一部分,还有一大堆东西,到时写好后可读性就太低了
#5
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
用Union查询太复杂了,我贴出来的只是一部分,还有一大堆东西,到时写好后可读性就太低了
还有没有更好的办法??
#6
如果你想写得简单,但是不在乎性能,你可以写
from x in table1
join y in table2 on x.MzRegId equals y.ID
from z in table3
x.MzRegId == table3.MzRegId || y.CardNo == z.CardNo
select new { x, y, z }
在table3数据量大的时候性能会比较差,反之无所谓。
或者你干脆用sql。
#7
linq对INNER JOIN的转化,是不支持OR的,但支持多个AND
你的情况可以用存储过程封装后在LINQ内调用
#1
本帖最后由 caozhy 于 2014-10-10 11:27:40 编辑
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
#2
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
#3
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
#4
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
用Union查询太复杂了,我贴出来的只是一部分,还有一大堆东西,到时写好后可读性就太低了
#5
from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on new { a = x.MzRegId, b = y.CardNo } equals new { a = z.MzRegId, b = z.CardNo }
select new { x, y, z }
你看清楚了, “ OR ” table2.CardNo = table3.CardNo 这里用的or 不是用and连接的
or更简单,可以这么写
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on x.MzRegId equals z.MzRegId
select new { x, y, z }).Union
(from x in table1
join y in table2 on x.MzRegId equals y.ID
join z in table3 on y.CardNo equals z.CardNo
select new { x, y, z })
用Union查询太复杂了,我贴出来的只是一部分,还有一大堆东西,到时写好后可读性就太低了
还有没有更好的办法??
#6
如果你想写得简单,但是不在乎性能,你可以写
from x in table1
join y in table2 on x.MzRegId equals y.ID
from z in table3
x.MzRegId == table3.MzRegId || y.CardNo == z.CardNo
select new { x, y, z }
在table3数据量大的时候性能会比较差,反之无所谓。