Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with LINQ

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
like image 337
Chuck Norris Avatar asked Dec 28 '11 07:12

Chuck Norris


People also ask

How to split a string into substrings using LINQ?

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

How to split a string into substrings of equal size?

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.

Is there a LINQ file format for comma separated values?

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.

How do you use multiple separator characters in a string?

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.


2 Answers

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(' ')....);
like image 97
Muhammad Adeel Zahid Avatar answered Sep 25 '22 00:09

Muhammad Adeel Zahid


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.

like image 35
Ani Avatar answered Sep 23 '22 00:09

Ani