Line extension method in Linq query

How to use line extension method in linq query:

public NewsType GetNewsType(string name) { var newsType = db.NewsTypes.FirstOrDefault(x => x.Name.ToFriendlyUrl() == name.ToFriendlyUrl()); return newsType; } 

The following x.Name.ToFriendlyUrl() request is not allowed per minute. Does anyone know how to handle this.

+4
source share
4 answers

it

 var newsType = db.NewsTypes.FirstOrDefault( x => x.Name.ToFriendlyUrl() == name.ToFriendlyUrl()); 

cannot be performed in the Entity Framework. ToFriendlyUrl - extension method. This is what is on the "client" computer. The query will be executed on the SQL server. The SQL server does not have a ToFriendlyUrl function.

The "standard" solution is to save the previously calculated version of ToFriendlyUrl() in the second column named FriendlyName , so your query will look like this:

 var friendlyName = name.ToFriendlyUrl(); var newsType = db.NewsTypes.FirstOrDefault( x => x.FriendlyName == friendlyName); 
+1
source

Assuming NewsTypes is IQueryable , this is the result of the fact that the Entity Framework will not be able to convert your extension method to SQL (how to do this?). If you cannot rewrite your predicate to something that the Entity Framework can translate to SQL, you will have to run the client side of the query:

 public NewsType GetNewsType(string name) { var newsType = db.NewsTypes.AsEnumerable().FirstOrDefault(x => x.Name.ToFriendlyUrl() == name.ToFriendlyUrl()); return newsType; } 

Note that AsEnumerable() was added before FirstOrDefault . Unfortunately, this can print all the lines returned by NewsTypes from server to client, and therefore can be quite expensive.

+1
source

Extension methods are indeed allowed in LINQ queries; moreover, LINQ methods are implemented as extension methods.

Another problem is to use extension methods (or most other methods) in LINQ to SQL or LINQ to-Entities queries. These queries are not actually executed in C # code, but they are treated as expressions translated into SQL. I.e.

 db.News.Where(x => x.Published).Select(x => x.Name) 

converted to SQL statement

 Select Name From News Where Published = 1 

and the results are returned in C # code.

Since there is no way to pass the ToFriendlyUrl() method to SQL, your code throws an error.

You have basically two solutions / workarounds. One of them is to convert the call into a form that can be translated into SQL, for example. if the ToFriendlyUrl() method was simple:

 public static string ToFriendlyURL(this string value) { return value.ToLower(); } 

you can embed this code in a LINQ call and it will work. If, however, the methods are more complex than your only solution is to simply extract the data from the database and then process it from C #:

 var newsTypeQuery = db.NewsTypes.Where(x => // other conditions, if any); var newsTypes = newsTypes.ToList(); //forces execution of the query // the result is now a C# list var newsType = newsTypes.FirstOrDefault(x => x.Name.ToFriendlyUrl() == name.ToFriendlyUrl()); 
+1
source

Try doing it instead

 public NewsType GetNewsType(string name) { var newsType = db.NewsTypes.FirstOrDefault(x => x.Name == name).ToFriendlyUrl(); return newsType; } 
0
source

Source: https://habr.com/ru/post/1497965/


All Articles