Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using my own method with LINQ to Entities

I have a project with LINQ and I want to use my own method in it. This NoWhiteSpaces method should return upper string with no spaces.

public static class LittleExtensions
{
    public static string NoWhiteSpaces(this String s)
    {
        return Regex.Replace(s, @"\s", string.Empty).ToUpper();
    }
}

When I want to use this method with LINQ, like this:

static void GetHaendler()
    {
        using (var riaService = new gkmRia())
        {
            var hladany = "someone";
            var haendlers = from hndlr in riaService.GetGkmHaendlerOutlet()
                            where hndlr.NameOutlet.NoWhiteSpaces() == hladany.NoWhiteSpaces()
                            select hndlr;
            Console.Write(haendlers.First().NameOutlet);
        }
    }

I get this error message:

LINQ to Entities does not recognize the method 'System.String NoWhiteSpaces(System.String)' method, and this method cannot be translated into a store expression.

Any solution? Thank you for your time.

like image 760
a.farkas2508 Avatar asked Jun 19 '13 09:06

a.farkas2508


People also ask

How do I use LINQ to entity?

LINQ to Entities Example The LINQ to Entities allows us to write queries (known as LINQ Queries) in C# and VB.NET. These LINQ queries are then transferred into the Expression Tree. The Database Provider (Entity Client in EF6) will then use the Entity Model to translate LINQ Queries into the SQL query.

How do I add a custom method to a LINQ query?

How to: Add Custom Methods for LINQ Queries (C#) You can extend the set of methods that you can use for LINQ queries by adding extension methods to the IEnumerable<T> interface. For example, in addition to the standard average or maximum operations, you can create a custom aggregate method to compute a single value from a sequence of values.

How do I use the query expression syntax in LINQ?

For examples that demonstrate how to use the query expression syntax, see the following topics: Another way to compose LINQ to Entities queries is by using method-based queries. The method-based query syntax is a sequence of direct method calls to LINQ operator methods, passing lambda expressions as the parameters.

What is the difference between LINQ and Entity SQL?

LINQ to Entities is a subset of LINQ which allows us to write queries against the Entity Framework conceptual models. Entity SQL is a query language designed to be used with the Entity Framework. The Entity SQL Queries are database independent. EntitySQL looks similar to standard SQL Queries Native SQL is the database dependent query language.


1 Answers

It is not possible to use custom methods or properties with LINQ to Entities, since LINQ to Entities needs to be able to translate the expression into a SQL statement which it can't do with your method.

A way to get around is to bypass LINQ to Entities and instead use LINQ to Objects by using Enumerable instead of Queryable (note the AsEnumerable() in the below code):

static void GetHaendler()
{
    using (var riaService = new gkmRia())
    {
        var hladany = "someone";
        var haendlers = from hndlr in riaService.GetGkmHaendlerOutlet().AsEnumerable()
                        where hndlr.NameOutlet.NoWhiteSpaces() == hladany.NoWhiteSpaces()
                        select hndlr;
        Console.Write(haendlers.First().NameOutlet);
    }
}

Note that this causes the query to execute in your application rather than in the database, so there may be some performance implications. If possible, it may be preferable to alter the query so it can be expressed using methods supported by the Entity Framework.

An alternative expression that is supported by LINQ to Entities could be as follows:

var hladany = "someone".NoWhiteSpaces();
var haenflers = from hndlr in riaService.GetGkmHaendlerOutlet().
                where hndlr.NameOutlet.Replace(" ", "").Replace("\t", "") == hladany
                select hndlr;

This example only handles spaces and tabs (the regex handles different whitespace characters as well), but I don't know your exact requirements so that may be sufficient. You can always chain more Replace calls to do additional replacements.

like image 61
Sven Avatar answered Sep 27 '22 19:09

Sven