Reputation: 360
How to write an expression for string concatenation for linq? I have this:
x => (x.Address1 + " " + x.Address2).Contains("add")
predicate and I don't know how to write a lambda expression for string concatenation without using string.Concat
method. So I have two expressions (x.Address1
and " "
) and I need to concatenate them.
Upvotes: 1
Views: 3374
Reputation: 1800
UPDATE: This link has the solution: NHibernate / MySQL string concatenation
Will the string foo
in .Contains(foo)
ever contain a space? If not, try Ade's second predicate as that avoids the concatenation.
If that doesn't work you might have to get more objects out of the database than you need and filter them in the code. If you have to do this it might be worth using an initial predicate like x => x.Address1.Contains(foo[0]) || x.Address2.Contains(foo[0])
to limit the number of objects you get back. I leave it to you to come up with a better initial predicate than this.
Upvotes: 1
Reputation: 2661
I can't see why you wouldn't want to use String.Concat. This works fine:
var foo = wibble.Select(s => String.Concat("foo", s, "bar"));
Going back to your example, wouldn't it be better to write x => x.Address1.Contains("add") || x.Address2.Contains("add")
Upvotes: 2