[英]Linq-to-SQL concatenate two columns in where clause
I have a table set up like so: 我有一个这样的表:
prefix | value
ABC 1234
ABC 5678
DEF 1234
Is it possible to create a linq query where prefix and value are concatenated for comparison in the where clause? 是否可以创建一个linq查询,其中将前缀和值连接在一起,以便在where子句中进行比较? I've tried this, but it always returns an empty set:
我已经尝试过了,但是它总是返回一个空集:
selected =
from i in dc.items
where i.prefix + i.value == "ABC1234"
select i;
Edit: the following T-SQL comes up with the correct results: 编辑:以下T-SQL给出了正确的结果:
WHERE LTRIM(RTRIM([prefix])) + LTRIM(RTRIM([value])) = 'ABC1234'
Edit2: The following, which combines most of the answers below, still does not work: Edit2:结合了以下大多数答案的以下内容仍然无效:
where (String.Concat(i.prefix.Trim(), i.value.Trim())) == "ABC1234"
Edit3: So I've got it working, but I have no clue why. Edit3:所以我已经开始工作了,但是我不知道为什么。 I've accepted an answer but if someone posts why it works I'd be grateful :)
我已经接受了答案,但是如果有人发布了它的工作原理,我将不胜感激:)
This works (returns n rows): 这有效(返回n行):
var temp = dc.items.Where(i => i.prefix.Trim() + i.prefix.Trim() == "ABC1234");
This does not work (returns 0 rows): 这不起作用(返回0行):
var temp =
from i in dc.items
where i.prefix.Trim() + i.value.Trim() == "ABC1234"
select i;
I dont's speak english. 我不会说英语。 Try this:
尝试这个:
var selected = dc.Where(x => x.prefix + x.value == "ABC1234");
Assuming that value
is not a string, you could concatenate them like this: 假设
value
不是字符串,则可以这样将它们连接起来:
var selected = dc.Items.Where(x => string.Concat(x.prefix, x.value) == "ABC1234");
The benefit to using string.Concat
is that all of the arguments will be converted to string. 使用
string.Concat
的好处是所有参数都将转换为字符串。
where (i.prefix + i.value) == "ABC1234"
Try to select this 尝试选择这个
selected =
from c in db.items
select new {
Name = string.Format("{0}{1}", c.prefix , c.value)
};
and look will you have "ABC1234" 看看你有“ ABC1234”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.