1
我有有相似的結構兩個表。每個表都有一個Id
和FirstValue
和SecondValue
領域。我想使用Linq-to-SQL來獲取TableOne
中的每一行,而不是TableTwo
(基於Id
),並且還獲取兩個表中的每一行,但具有不同的值FirstValue
或SecondValue
。在SQL應該像獲取不同的值或缺少的行從一個表
SELECT TableOne.*
FROM TableOne
LEFT OUTER JOIN TableTwo
ON TableOne.Id = TableTwo.Id
WHERE TableTwo.Id IS NULL
OR (TableTwo.Id IS NOT NULL AND
(TableOne.FirstValue <> TableTwo.FirstValue OR
TableOne.SecondValue <> TableTwo.SecondValue
)
)
我已經試過
(from a in context.TableOne
from b in context.TableTwo.Where(b =>
a.Id == b.Id &&
(a.FirstValue != b.FirstValue ||
a.SecondValue != b.SecondValue)).DefaultIfEmpty()
select a).ToList();
在該查詢的問題是,如果TableOne
和TableTwo
有匹配的值,該行仍然得到恢復,但所有的TableTwo
的值爲空。如果一行在每個表中都有相同的值,我希望它不會被返回。
謝謝。這工作完美。 –