1

我有一张这样的桌子:

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;
4

4 回答 4

4

我不会说英语。试试这个:

var selected = dc.Where(x => x.prefix + x.value == "ABC1234");
于 2012-04-24T21:49:35.033 回答
2

假设这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.

于 2012-04-24T21:59:21.233 回答
0
where (i.prefix + i.value) == "ABC1234"
于 2012-04-24T21:52:06.633 回答
0

尝试选择这个

selected =
 from c in db.items

 select new {

 Name = string.Format("{0}{1}", c.prefix , c.value)

};

看看你有没有“ABC1234”

于 2012-04-24T21:57:12.877 回答