用我自己的方法与LINQ到实体 [英] Using my own method with LINQ to Entities

查看:142
本文介绍了用我自己的方法与LINQ到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有LINQ一个项目,我想用我自己的方法吧。这NoWhiteSpaces方法应该返回上字符串没有空格。

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();
    }
}

当我想用这种方法使用LINQ,像这样的:

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实体无法识别方法'System.String NoWhiteSpaces(System.String)'方法,和这种方法不能被翻译成商店表达

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.

推荐答案

这是不可能使用自定义方法或属性与LINQ到实体,因为LINQ到实体需要能够翻译表达成一个SQL语句,它不能用你的方法做的。

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.

要解决的一种方法是绕过LINQ到实体,而是通过使用可枚举而不是可查询使用LINQ到对象(注意 AsEnumerable()在下面的代码):

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.

这是通过LINQ支持实体可能是如下的另一种表达:

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.

这篇关于用我自己的方法与LINQ到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆