开发者

How to search for same search string in multiple columns?

I am searching for string input from textbox control in EF data model. Could someone help me to construct the where clause in the code file to do this. I tried the below piece of code in the code, even though it compiles throws an error when I enter the search string and submit the search.

Where("it.[CaseName] like '%'" + searchString +
    "'%' or it.[CaseNumber] like '%'" + searchString +
    "'%' or it.[AppRegNumber] like '%'" + searchString +
    "'%' or it.[SSNo] like '%'" + searchString + "'%'")

When this same where clause was used in EntityDataSource control markup it accepts and search cor开发者_StackOverflowrectly. I am in the process of changing my pages to use ObjectDataSource control to connect to a business logic layer class instead of directly to EDM data model.


If you want to use the Where Query Builder method of ObjectQuery (I think that's what you are using in your question), you need to remove the inner single quotes from the query expression:

Where("it.[CaseName] like '%" + searchString +
    "%' or it.[CaseNumber] like '%" + searchString +
    "%' or it.[AppRegNumber] like '%" + searchString +
    "%' or it.[SSNo] like '%" + searchString + "%'")

Or as a parametrized query:

Where("it.[CaseName] like @search" +
    " or it.[CaseNumber] like @search" +
    " or it.[AppRegNumber] like @search" +
    " or it.[SSNo] like @search",
    new ObjectParameter("search", string.Concat("%", searchString, "%")))

Edit

Don't use the first version if searchString comes from user input on a web page as this version is vulnerable to SQL Injection (enter this term in search engine for explanation). Instead use the parametrized query in the second version.


Well, you can use the Linq extension methods:

.Where(r => r.CaseName.IndexOf(searchString) >= 0
    || r.CaseNumber.IndexOf(searchString) >= 0
    || r.AppRegNumber.IndexOf(searchString) >= 0
    || r.SSNo.IndexOf(searchString) >= 0)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜