I want to order by my results with the matches count in my string line.
So here is code
.ThenByDescending(p => p.Title.ToLower()
.Split(' ')
.Count(w => words.Any(w.Contains)));
But it bring me error and says that LINQ can't parse Split
into SQL.
LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, and this method cannot be translated into a store expression.
How can I implement Split via LINQ?
For example, for this array it must order in this way
words = { "a", "ab" }
ab a ggaaag gh //3 matches
ba ab ggt //2 matches
dd //0 matches
1 Using LINQ We can use LINQ’s Select () method to split a string into substrings of equal size. ... 2 Using String.Substring () method Another solution is to simply use the String.Substring () method to break the string into substrings of the given size, as shown below: 1 2 ... 3 Using Regex
Using LINQ We can use LINQ’s Select () method to split a string into substrings of equal size. The following code example shows how to implement this: 2.
However, you if cannot (or you don't want to) do it, then have a look at this page: LINQ: Query Comma Separated Value (CSV) files [ ^ ]. The content must be between 30 and 50000 characters.
String.Split can use multiple separator characters. The following example uses spaces, commas, periods, colons, and tabs, all passed in an array containing these separating characters, to Split. The loop at the bottom of the code displays each of the words in the returned array.
it means that Linq to entities failed to find translation of split method that can be written as sql query. if you want to perform split functions you have to bring the record in memory by calling ToList()
, AsEnumerable()
etc.
var result = (from t in db.Table
select t).AsEnumerable().OrderBy(x=>x.Column).ThenByDescending(p=>p.Title.ToLower.Split(' ')....);
One can't expect LINQ to Entities to be able to convert that to SQL.
The best solution is to change the schema such that each word in a post's title is stored as a separate row in a separate table (with the appropriate associations). The query can then use explicit (group) join operations or the FK association property.
If you can't do this and yet want the query to run on the database, you'll have to look into writing a user-defined function to work with delimited strings. Read this question for more info. This will be a lot of work though.
The easiest solution (if you can afford to do it) of course is to pull everything back to the client just use LINQ to Objects for that part of the query, as mentioned by @Muhammad Adeel Zahid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With