我有一个像这样设置的表:
prefix | value
ABC 1234
ABC 5678
DEF 1234
是否可以创建一个 linq 查询,其中连接前缀和值以在 where 子句中进行比较?我试过这个,但它总是返回一个空集:
selected =
from i in dc.items
where i.prefix + i.value == "ABC1234"
select i;
编辑:下面的 T-SQL 给出了正确的结果:
WHERE LTRIM(RTRIM([prefix])) + LTRIM(RTRIM([value])) = 'ABC1234'
Edit2:以下结合了下面的大部分答案,仍然不起作用:
where (String.Concat(i.prefix.Trim(), i.value.Trim())) == "ABC1234"
Edit3:所以我让它工作了,但我不知道为什么。我已经接受了一个答案,但如果有人发布它为什么有效,我将不胜感激:)
这有效(返回 n 行):
var temp = dc.items.Where(i => i.prefix.Trim() + i.prefix.Trim() == "ABC1234");
这不起作用(返回 0 行):
var temp =
from i in dc.items
where i.prefix.Trim() + i.value.Trim() == "ABC1234"
select i;
最佳答案
我不会说英语。试试这个:
var selected = dc.Where(x => x.prefix + x.value == "ABC1234");
关于c# - Linq-to-SQL 连接 where 子句中的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306437/