LINQ2SQL通过多个列加入(通过运算符)?[英] Linq2Sql join by multiple columns (by OR operator)?

本文是小编为大家收集整理的关于LINQ2SQL通过多个列加入(通过运算符)?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

割草机我可以翻译以下内容:

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

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

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

我知道,我可以通过工会建设做这种肮脏的事情,但是有比复制大多数查询更好的方法吗?

推荐答案

您在Linq-to-sql中不能使用or> on子句.您需要做:

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

本文地址:https://www.itbaoku.cn/post/1556858.html

问题描述

Mow I can translate this:

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

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 ...;

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

推荐答案

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 };

Equivalent to the sql of:

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