Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String extension method in Linq query

How to use string extension method in linq query:

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

Above query x.Name.ToFriendlyUrl() is not allowed at the minute. Is anyone know how to achieve with it.

like image 443
Tun Avatar asked Aug 20 '13 10:08

Tun


People also ask

What is extension method in LINQ?

LinqExtensionMethod.zip. Linq provides standard query operators like filtering, sorting, grouping, aggregation, and concatenations, and it has many operators to achive many types of functionalities, which are called extension methods, in LINQ.

What are extension methods in C#?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Can you use LINQ on a string?

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions.

Which extension method do you need to run a parallel query in Plinq?

AsParallel extension method on the source sequence and executing the query by using the ParallelEnumerable. ForAll method. This documentation uses lambda expressions to define delegates in PLINQ.


1 Answers

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

It's quite another issue however, to use extension methods (or most other methods) in LINQ-to-SQL or LINQ-to-Entities queries. Those queries are not actually run in the C# code, but they are treated like expressions that are translated to SQL. I.e.

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

is translated to the SQL Statement

Select Name 
From News
Where Published = 1

and it's results are returned to the C# code.

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

You have basically, two solutions/workarounds. One is to transform the call to a form could be translated into SQL, e.g. if the ToFriendlyUrl() method was just:

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

you can inline that code in the LINQ call, and that would work. If however, the methods is more complex, than your only solution is to just fetch the data from the base and then process it on the C# side:

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());
like image 150
SWeko Avatar answered Oct 09 '22 22:10

SWeko