Entity Framework 4 搜索组合字段
如何搜索两个组合字段。如果可能的话,搜索应该发生在 SQL 端。
假设我有一个包含名字和姓氏的客户表。我希望用户能够使用单个搜索框搜索这两列。
我的查询目前看起来像这样:
var query = DbContext.Customers
.Where(c => c.FirstName.Contains(search) || c.LastName.Contains(search));
但它应该是这样的
var query = DbContext.Customers
.Where(c => c.FullName.Contains(search));
How do I search on two combined fields. The search should happen on the SQL end if possible.
So say I have a customer table with first name and last name. I would like users to be able to search on both of those columns using a single search box.
My query currently looks like this:
var query = DbContext.Customers
.Where(c => c.FirstName.Contains(search) || c.LastName.Contains(search));
but it should be something like
var query = DbContext.Customers
.Where(c => c.FullName.Contains(search));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您还映射了
FullName
列,否则这是不可能的。解决此问题的方法可以是 Linq-to-entities 中允许的 String.Concat:It is not possible unless you have
FullName
column also mapped. The way around this problem can be String.Concat which is allowed in Linq-to-entities:您可以使用数据库中的计算列并将其映射
例如
(我知道不太漂亮)
You could use a computed column in the database and map that
e.g.
(Not pretty I know)