I'm getting the error:
LINQ to Entities does not recognize the method 'Int32 LastIndexOf(System.String)'
method, and this method cannot be translated into a store expression.
When using this code to tell if a person's last name starts with certain characters:
persons = persons.Where(c => c.FullName.IndexOf(" ") > 0 &&
c.FullName.Substring(c.FullName.LastIndexOf(" ")+1).StartsWith(lastNameSearch));
Any clue how to achieve this without using LastIndexOf()? Maybe I have to check for this after I grab results from the database using ToList()?
You are limited by the set of canonical functions that can be translated into an SQL query, so any solution must be achieved with no more than the canonical functions offer.
Luckily, one of the supported functions is the bool Contains(string) instance method. You can rewrite your check as
persons = persons.Where(c => c.FullName.Contains(" " + lastNameSearch));
This is not exactly like your current version (because it will allow people with more than one name to match their second name, while the former won't), but it's pretty close and IMHO can be acceptable.
Of course it would be much better than any of this to keep the last names as a separate column in the database, if that is at all possible.
Related
Please tell me how to solve this problem.
Here is an example table:
Filled with data like this:
I have a method to automatically assign a number to the CerNambe column:
public string CerNamber()
{
var cer = DBContext.Certificate
.Where(p => p.CerNambe != null)
.OrderByDescending(p => p.Id)
.FirstOrDefault();
string _cer = cer.CerNambe;
int outCer = Convert.ToInt32(_cer.Substring(0, _cer.IndexOf('-')));
string newCer = Convert.ToString(outCer + 1 + "-P/" + DateTime.Now.Year);
return newCer;
}
But I ran into a problem. If the number is assigned by the user erroneously, it is deleted, and the numbering is violated.
Here is the question: how to find this violation and assign the missing number to the next record. But in this case, automatic numbering will not work? Since the method is looking for the last record!
How to find this violation and assign the missing number to the next record ?
If I were you, I would modify the database schema so that I don't have to do this. But if you have to, the easiest yet ugliest way is to use RegEx to filter and find special patterns.
The solution: If your underlying database provider is SQL Server, you could use SqlMethods.Like to filter the database result set down to a manageable subset of data that can then analyze locally with RegEx.
Linq to Entities doesn't support regex because it can't convert it to SQL. You could load all of the records back into memory and then use Linq to Objects on them (perform a. ToList() to a variable, then a second LINQ query for this regex), but this meant you'll get to load every DB record into memory to run this query.
The same advice applies: modify the database schema so that you don't have to do this.
For understanding RegEx visit :
System.Text.RegularExpressions.Regex Class
I have a list of strings that are search Queries.
I want to see if a string from the database contains anyone of those terms in the Query. I'd like to do this on one line of code, that doesn't make multiple calls to the database. This should work but I want it to be more optimized.
var queries = searchQuery.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries).Distinct();
var query = context.ReadContext.Divisions.AsQueryable();
queries.ForEach(q => {
query = query.Where(d => (d.Company.CompanyCode + "-" + d.Code).Contains(q));
});
Is there a function that can do this better or a more optimal way of writing that?
There are two issues with your proposed solution:
Most LINQ to SQL providers don't understand string.Contains("xyz") so the provider will either throw an exception or fetch all the data to your machine. The right thing to do is to use SqlMethods.Like as explained in Using contains() in LINQ to SQL
Also, the code you show will check whether the division contains all of the specified strings.
To implement the 'any' behavior you need to construct a custom expression, which will not be possible using plain C#. You would need to look at the System.Linq.Expressions namespace: https://msdn.microsoft.com/en-us/library/system.linq.expressions(v=vs.110).aspx
It is possible, but quite involved.
Say I have a table Table1 with a string field [ProductString] with values:
Alpha, alphanumeric or numeric: eg ABC, B4, U2, C 5, 100, U1, U5, U6, U11
I want to be able to take a where clause like "ProductString >= U5", and pass this to a LINQ statement as a string so it evaluates
Table1.Where(t=> t.ProductString >= 'U5');
Normally this would return results U5 and U6.
However, this I want to be able to use a NaturalSortComparer somehow so that the results returned are U5, U6 and U11.
I know how to use the comparer in an OrderBy, by I wanted to be able to use it at the Where stage.
Using natural sort comparer:
var comparer = new NaturalComparer();
Table1.Where(t=>
comparer.Compare(t.ProductString, "U5") >= 0);
Presuming all your product strings is on the format U%number% then why not abuse that fact?
Table1.Where(t=> int.Parse(t.ProductString.Replace("U","")) >= 5);
If you're using LINQ to Entities I'm not certain this will compile to a store expression (i.e that SQL knows what to do with this - I guess it should).
I'm a little confused, given the accepted answer, about whether this question relates to LINQ to Entities or not. The accepted answer doesn't appear to be a solution that would work in the LINQ to Entities context, but the comments on the question by the OP seem to confirm that this is being executed in the database context. Anyway, this answer is specifically targeted toward LINQ to Entities.
I think doing this in SQL Server would be hard, but not impossible. The problem is that .NET knows what NaturalSortComparer is, but SQL Server (where you want the query to ultimately take place) has no such concept. The best idea I can think of would consist of 2 parts:
Create a UDF (User Defined Function) in SQL server that will give a product that is orderable via natural sort: CREATE FUNCTION Naturalize(#val as nvarchar(max)) RETURNS nvarchar(1000). There's a pretty cool answer here that creates a UDF wrapper around a CLR function to accomplish just that.
Next create a function mapping for your DbContext that maps the UDF above to a function that can be called inside an EF query against the DbContext. Something like this:
[DbFunction("MyContext", "Naturalize")]
public static string Naturalize(this string value)
{
throw new NotSupportedException("This function can only be invoked from LINQ to Entities.");
}
Once you've got these two pieces in place, you can readily use this new function inside an entity query to compare strings using the Naturalized value in the comparison:
Table1.Where(t=> t.ProductString.Naturalize() >= "U5".Naturalize());
Bear in mind that the UDF will be executed against every row contained in the query, which is the whole table in the above example. You'll want to make sure to pare down your query to something manageable before applying the function as a sub-query. Or you may want to try applying some type of UDF-based index on the table in question.
If you are going to be doing searches like this a lot, then what will be the best thing to do is add two new fields to your table, [ProductCode] & [ProductNumber] which separate the two parts of the [ProductString].
Then you comparison becomes:
Table1.Where(t=> t.ProductCode == "U" && t.ProductNumer > 5);
I am usins LINQ TO ENTITY in order to filter a list of persons based on their phone number.
IEnumerable<Person> personsList = (from person in repoPersons.All()
where person.PhoneNumber.Contains(PhoneSearch)
select person).ToList();
repoPersons.All() contains all the persons read from the database.
PhoneSearch is the search term entered by the user.
The Phone number is saved in the database as a string in a special format: +1 (555) 555-6608 (as an example.
But I want to allow the user to search for 5555556608 without having to format it properly. (I want him to be able to search for part of the phone number too like 556608 and still get the result.
I tried to create a Method called RemoveSpecialCharactersInPhoneNumber that will remove the special characters and returns it and use it like this:
IEnumerable<Person> personsList = (from person in repoPersons.All()
where RemoveSpecialCharactersInPhoneNumber(person.PhoneNumber).Contains(PhoneSearch)
select person).ToList();
But I get this error:
LINQ to Entities does not recognize the method 'System.String RemoveSpecialCharactersInPhoneNumber(System.String)' method, and this method cannot be translated into a person expression.
So is there a way I can check if the PhoneNumber contains PhoneSearch without the special character, using LINQ TO ENTITY?
I know I can use LINQ TO ENTITY to get all the list and then use LINQ TO OBJECT to filter it but I am trying to avoid this approach.
Any solution or hint is greatly appreciated
The following isn't exactly neat and tidy, but it will solve your problem (as no method is trying to be incorrectly resolved on the SQL end)
IEnumerable<Person> personsList = (from person in repoPersons.All()
where person.Phone.Replace("+", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Contains(PhoneNumber)
select person).ToList()
It will give error because the linq to entity will be converted into SQL query so there is no method for RemoveSpecialCharactersInPhoneNumber in sql.
This is similar question.
Seems like as where clause is evaluated on server side, this method cannot be recognized.
CLR method needs to be converted to Cannonical mapping so that it can be executed on server.
Try doing
IEnumerable<Person> personsList = (from person in repoPersons.All().ToList()
where RemoveSpecialCharactersInPhoneNumber(person.PhoneNumber).Contains(PhoneSearch)
select person).ToList();
This will prevent LINQ from trying to resolve RemoveSpecialCharactersInPhoneNumber in the SQL layer, and instead it can be processed by your business layer as you described.
So, in my last post I was asking how to build a dynamic search filter using LINQ and EF4 (See Here) and finally came up with the solution of building the expression as a string and parse it to an expression using the Dynamic LINQ library.
I that solved the problem. I was able to generate a Expression<Func<TSource, out bool>> and pass it to the Where() method of the DbSet. I am also trying to do this using MySql as a database behind EF4.
The problem came when I tried to apply string operations to integers, like searching a database record which consecutive number starts with 1234.
My initial expression was something like: record.ConsecutiveNumber.ToString().StartsWith("1234"). Sadly, as expected, things were not that easy as EF4 fails to query the DbSet with exception:
"LINQ to Entities does not recognize
the method 'System.String ToString()'
method, and this method cannot be
translated into a store expression."
After some Google search I found that this is a common problem. But C'MON! Is there a way to perform a search function that can search records with a consecutive number starting by "1234"?
How pros implement search features with EF4? This is with a single property filter. What if I wanna add multiple filters? God, my head hurts... :/
Thanks!
EDIT:
Thought #1: What about a stored procedure? What about calling a MySql stored procedure from Linq? Am I aiming way too high?
You can use the SqlFunctions.StringConvert method. It requires a double (or decimal) so you'll have to cast your int ConsecutiveNumber.
Replace:
record.ConsecutiveNumber.ToString().StartsWith("1234")
With:
SqlFunctions.StringConvert((double)record.ConsecutiveNumber).StartsWith("1234")
Have you looked at the Dynamic LinQ Library:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
And for your question
How to use "contains" or "like" in a dynamic linq query?
Previously I have gotten the code for this lib and just taken a look inside, it is pretty easy to follow.
This would be my thought process on getting it to work. Hopefully it points you in the right direction.
According to other posts SqlFunctions.StringConvert((double)record.ConsecutiveNumber) works for Sql Server.
Problem with converting int to string in Linq to entities
And here is relevant information on linq conversions.
Linq int to string
And here is an answer hinting at writing your own sql function for stringconvert
Using a SQL Function in Entity Framework Select
If SqlFunctions.StringConvert doesn't work for you I'd suggest looking at figuring out how to do it in Sql and then writing your own [EdmFunction()] attribute based method.
I haven't got a clue if this will work over Linq to EF or not but presuming that they mapped the Math operations, this might solve your need:
record.ConsecutiveNumber / Math.Pow(10, Math.Truncate(Math.Log10(record.ConsecutiveNumber) - 3)) == 1234
This is basically dividing the number by a power of 10 just big enough to leave the first 4 digits.
I know this is very hacky and inefficient even if it works, but there you go. :)
Any method calls in a LINQ to Entities query that are not explicitly mapped to a canonical function will result in a runtime NotSupportedException exception being thrown.
Check mapping canonical function here:
http://msdn.microsoft.com/en-us/library/bb738681.aspx
In this case, you can use Math function. (I don't think code first can use in product project at that time)