Linq2Sql通过多列联接(通过OR运算符)? [英] Linq2Sql join by multiple columns (by OR operator)?

查看:68
本文介绍了Linq2Sql通过多列联接(通过OR运算符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我可以翻译这个:

SELECT * 
FROM vectors as v
INNER JOIN points as p 
ON v.beginId = p.id OR v.endId = p.id

进入linq2sql语句?基本上我想要这个:

Into linq2sql statement? Basically I want this:

var query = from v in dc.vectors
            join p in dc.points on p.id in (v.beginId, v.endId)
            ...
            select ...;

我知道,我可以通过Union构造来做到这一点,但是有比复制大多数查询更好的方法吗?

I know, I can do this dirty through Union construction, but is there a better way than duplicating most of the query?

推荐答案

在带有or的linq-to-sql中不能有on子句.您需要这样做:

You can't have an on clause in linq-to-sql with an or. You need to do:

var result = from v in dc.vectors
             from p in dc.points
             where p.id == v.beginId || p.id == v.endId
             select new { v, p };

等同于sql:

SELECT * 
FROM vectors as v,
     points as p 
WHERE v.beginId = p.id 
OR v.endId = p.id

这篇关于Linq2Sql通过多列联接(通过OR运算符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆