所以我想看看一个字符串是否存在于多个用逗号分隔的串联列上:
例如,我的表有列地址
、城市
、状态
如下所示:
因此逗号分隔的串联字符串如下所示:
123 St., Farmtown, MN
456 Road, Austin, TX
789 Way, Emerald, RH
注意每个逗号后面的空格
使用LINQ方法语法,以便能够输入像“st,farm”这样的字符串并获得第一行
var inputString = "St., Farm"
Expression<Func<MyTable, bool>> validAddressInput =
x => string.Concat(x.Address, ", ", x.City, ", ", x.StateProvince)
.Contains(inputString);
var results = _context.MyTable.Where(validAddressInput).ToList()
// results should contain that 1st row
InvalidOperationException - The LINQ expression ' ... ' could not be translated.
Either rewrite the query in a form that can be translated
or switch to client evaluation explicitly by inserting a call to 'AsEnumerable' 'AsAsyncEnumerable' 'ToList' or 'ToListAsync'.
很明显,它似乎无法将string.concat(...)
转换为原始SQL,但我不确定将其更改为什么。
因此,虽然string.concat
和interpolation$“{...}”
不起作用,但只需在字符串之间使用+
即可:
Expression<Func<MyTable, bool>> validAddressInput =
x => (x.Address + ", " + x.City + ", " + x.StateProvince)
.Contains(inputString);