views:

387

answers:

2

Hi,

I'm trying to accomplish the following query (notice .StartsWith):

return (from p in _session.Linq<Profile>()
        where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
        select p).ToList();

This throws: could not resolve property: Firstname.Lastname.

If I do this:

return (from p in _session.Linq<Profile>()
        where p.Firstname.StartsWith(wildcard)
        select p).ToList();

Everything is working. How can this be?

Thanks in advance!

A: 

Update: rewritten answer based on new insights and edited questions.

What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:

The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?

Abel
Ooops, a little hard copy/paste :P
J. Hovgaard
Allright, I got a little further. I'm nearly convinced that the error is because of a bug in Linq to NHibernate. When the query is "converted" by L2NHibernate it is actually requesting Firstname.Lastname instead of concatenating the two properties.
J. Hovgaard
+2  A: 

The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.

Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.

Is this what you want?

return (from p in _session.Linq<Profile>()
        where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
        select p).ToList();
Daniel A. White
Allright, that makes sense!But if I can't do it this way, do you have another way to accomplish what I wanna do?
J. Hovgaard