开发者

Linq To NHibernate: .StartsWith on multiple properties

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

return (from p in _session.Linq<Profile>()
        where (p开发者_JS百科.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!


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();


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜