Scenario:
const string srchFormatter = "{0} {1} {2}";
var result = from c in db.ContactsDumps1s
where string.Format(srchFormatter, c.FirstName, c.MiddleName, c.LastName).Contains(txtSearch.Text.Trim())
select new { c.FirstName, c.MiddleName, c.LastName };
Error:
Method ‘System.String Format(System.String, System.Object, System.Object, System.Object)’ has no supported translation to SQL.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.Exception Details: System.NotSupportedException: Method ‘System.String
Format(System.String, System.Object, System.Object, System.Object)’
has no supported translation to SQL.
Can anybody please let me know why am I not able to search in this manner & any alternatives for this search?
This is what I plan to achieve :
select FirstName, MiddleName, LastName
from ContactsDumps1
where FirstName+MiddleName+LastName like '%Search_String%'
by using LINQ.
Seems like you’re doing a LINQ-to-SQL query, since your DB context is tied to a database connection. In doing this, you’re issuing a query to your database. Think about the query that you’re constructing:
Which probably translates to something like:
That doesn’t really translate well. I think what you’re trying to do is a LIKE – where
FirstName Starts With SomeTextValue.So you may want to try:
I may not understand what you’re exactly trying to do, but hopefully this helps. Let me know if I’m missing something and I’ll clarify my answer.
UPDATE: I updated my suggested LINQ statement to search FirstName, MiddleName or LastName for the text in txtSearch.
UPDATE #2: I noticed your updated question and what you’re looking to do. Here’s an approach that may work for you:
Using LINQPad, I did something similar and the generated SQL from it is:
That roughly translates to what you want to do.
Hope this helps.