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

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

问题描述

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

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

当我想使用LINQ这个方法,这个:

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

我收到此错误消息:


LINQ to Entities不能识别方法'System.String NoWhiteSpaces(System.String)'方法,并且此方法不能转换为存储表达式。 / p>

任何解决方案?谢谢你的时间。

解决方案

从LINQ to Entities起,LINQ to Entities不可能使用自定义的方法或属性需要能够将表​​达式转换为不能用您的方法执行的SQL语句。



解决方法是绕过LINQ to Entities使用枚举而不是可查询(注意 AsEnumerable()在下面的代码):

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

请注意,这会导致查询在您的应用程序中执行而不是在数据库中,所以可能会有一些性能影响。如果可能,最好更改查询,以便可以使用支持的方法来表达查询由实体框架



LINQ to Entities支持的替代表达式可以如下:

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

此示例仅处理空格和制表符(正则表达式也处理不同的空格字符),但我不了解您的具体要求,这样就足够了。您可以随时链接更多替换调用以执行其他替换。


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.

解决方案

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.

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

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