在添加这个问题之前,我确实在 stackoverflow 上搜索过类似的问题,但我找不到。互联网上的大多数问题都使用带有字符串的 LIKE(例如 LIKE '%ABC%'),但我需要与不同表的现有列进行比较。
我需要为 select 语句编写一个 linq 查询,如下所示 -
select *
from [dbo].[BaseClaim]
where WPId like (select WPId from UserProfiles where ID='1459')
我想出了下面的 linq 查询,但它没有按预期工作 -
var result = (from claimsRow in context.BaseClaims
where (from upRow in context.UserProfiles
where upRow.ID == 1459
select upRow.WPId).Contains(claimsRow.WPId)
select claimsRow);
上面linq生成的sql如下——
SELECT
[Extent1].[WPId] AS [WPId]
FROM [dbo].[BaseClaim] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM (SELECT
[UserProfiles].[ID] AS [ID],
[UserProfiles].[WPId] AS [WPId]
FROM [dbo].[UserProfiles] AS [UserProfiles]) AS [Extent2]
WHERE (1459 = [Extent2].[ID]) AND ([Extent2].[WPId] = [Extent1].[WPId]))
所以很明显,我的 linq 无法将 baseclaim.wpID 与 userprofiles.wpid 进行比较,而不是 LIKE。