LINQ 2 SQL查询不以函数调用工作 [英] LINQ 2 SQL query does not work with a function call

查看:133
本文介绍了LINQ 2 SQL查询不以函数调用工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定这个问题已经被问了几次,我也为apolgize再次询问,但我做了一些研究,不幸的是我没有找到我在互联网上的乐趣...

I am quite sure this question has already been asked several times and I do apolgize for asking it once again, but I made a few research and unfortunately I did not find my pleasure on the internet...

我有一个IQueryable的是这样的:

I have a IQueryable like this :

triggers = triggers.Where(t => GetIdFromName(t.Name) == filter.Id.ToString());



功能GetIdFromName得到名称的一部分来检索ID:

The function GetIdFromName get a part of the name to retrieve the Id :

public string GetIdProfiloFromName(string name)
        {
            return name.Substring(name.IndexOf(NameFirstSeparator)+1,name.IndexOf(NameSecondSeparator)-name.IndexOf(NameFirstSeparator)-1);
        }



我知道这是不是很好,但我可以设法最好这样做为止。我的问题是,使用LINQ to SQL中使用这两个语句是不允许的。应用程序将引发一个错误,而这是确定:

I know it is not nice, but it is the best I could managed to do so far. My question is that using Linq to sql is not permitted using these two statements. The application throws an error, whereas this is ok :

triggers = triggers.Where(t => t.Name.Substring(t.Name.IndexOf(NameFirstSeparator) + 1, t.Name.IndexOf(NameSecondSeparator) - t.Name.IndexOf(NameFirstSeparator) - 1) == filter.Id.ToString());



我怀疑是功能GetIdFromName应该给的东西不是一个字符串不同,但我真的不知道什么和如何...

I suspect that the function GetIdFromName should give something different than a string, but I really wonder what and how...

你的启蒙,

约恩·


更新到我的理解至今:

Update to what I understood so far:

我不能做什么,我想要的,因为为了做到这一点,我需要做这样的事情:

I cannot do what i wanted, because in order to do so I would need to do something of this kind :

static Expression<Func<Trigger,string,  bool>> IsId = (a,b) => a.name.Substring(a.name.IndexOf(NameFirstSeparator) + 1, a.name.IndexOf(NameSecondSeparator) - a.name.IndexOf(NameFirstSeparator) - 1)==b;



不过,这不会进入

But this would not get into

triggers = triggers.Where(func<Trigger,bool>);

和我可以想办法做到这一点,但我会得到所有从我的数据库中的结果执行我的记忆测试。

And I could manage to do it, but I would have to get all the results from my database to perform my test in memory.

非常感谢所有的,你这真的让我清楚!

Thanks a lot all, you made this get really clear to me!!

推荐答案

2的LINQ SQL的转换查询到的表达式树,然后把这一表达式树成SQL。由于您的自定义功能不具有SQL相关的功能,它不知道如何把它翻译并抛出异常。

LINQ 2 SQL is converting your query into an expression tree, and then translating that expression tree into SQL. Since your custom function doesn't have an SQL correlated function, it doesn't know how to translate it and throws an exception.

这篇关于LINQ 2 SQL查询不以函数调用工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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