我有一个SQL查询,它基本上通过两个键将MyWords表连接到MyTranslations表。这里是我的表格,以便更好地理解。
MyWords表:
Id
WordString
LangType
MyTranslations表:
Id
WordColumnAId
WordColumnBId
请注意,WordColumnAId
和WordColumnBId
列是代表MyWords.Id
的fk。My SQL query:
SELECT MyTranslations.Id
,WordColumnAId
,WordColumnBId
,MyWords.WordString
,MyWords.LangType
FROM MyTranslations
join MyWords on (MyTranslations.WordColumnAId = MyWords.Id or MyTranslations.WordColumnBId = MyWords.Id)
where MyWords.LangType !=@currentLangType
我知道乍一看使用多个键的join on
是没有意义的,但我正试图进行交叉查询,将两个表连接起来,无论键是在WordColumnAId
还是WordColumnBId
上。
我正试图将上面的T-SQL查询调整为LinQ查询。问题是,我找不到我的方式在LinQ使用两个键在一个单一的连接查询。这是我到目前为止得到的:
from translation in queryableTranslation
join word in _myWordRepository on translation.WordColumnAId equals word.Id // This is where I want to add `or translation.WordColumnBId equals word.Id` but get errors.
where word.LangType != currentLangType
select new QueryResultDto {
MyTranslationId = translation.Id,
WordString = word.WordString,
LanguageType = word.LangType,
WordColumnAId = translation.WordColumnAId,
WordColumnbId=translation.WordColumnbId,
};
我对LinQ很陌生,正在努力学习。所以我的问题是:有没有办法在LinQ中实现这一点,或者我在尝试不可能的事情?我也愿意接受更好的方法。
标题>EF和其他LINQ提供程序在使用这种语法时应该将查询转换为INNER JOIN:
var query =
from translation in queryableTranslation
from word in _myWordRepository.Where(word => translation.WordColumnAId == word.Id
|| translation.WordColumnBId = word.Id)
where word.LangType != currentLangType
select new QueryResultDto
{
MyTranslationId = translation.Id,
WordString = word.WordString,
LanguageType = word.LangType,
WordColumnAId = translation.WordColumnAId,
WordColumnbId=translation.WordColumnbId,
};