LINQ查询中的自定义方法 [英] Custom Method in LINQ Query

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

问题描述

我将自己归结为不幸的是,LINQ to EF查询中的自定义方法令人困惑.我浏览了一下网络,尝试检测一种使自定义方法对LINQ友好的模式,并且每个消息源都说该方法必须为translatable into a T-SQL query,但应用程序看起来非常多样化.因此,我将在此处发布代码,并希望慷慨的SO denizen可以告诉我我做错了什么以及为什么.

I sum myself to the hapless lot that fumbles with custom methods in LINQ to EF queries. I've skimmed the web trying to detect a pattern to what makes a custom method LINQ-friendly, and while every source says that the method must be translatable into a T-SQL query, the applications seem very diverse. So, I'll post my code here and hopefully a generous SO denizen can tell me what I'm doing wrong and why.

    public IEnumerable<WordIndexModel> GetWordIndex(int transid)
    {
        return (from trindex in context.transIndexes
                let trueWord = IsWord(trindex)
                join trans in context.Transcripts on trindex.transLineUID equals trans.UID
                group new { trindex, trans } by new { TrueWord = trueWord, trindex.transID } into grouped
                orderby grouped.Key.word
                where grouped.Key.transID == transid
                select new WordIndexModel
                {
                    Word = TrueWord,
                    Instances = grouped.Select(test => test.trans).Distinct()
                });
    
    }

    public string IsWord(transIndex trindex)
    {
        Match m = Regex.Match(trindex.word, @"^[a-z]+(\w*[-]*)*",
                              RegexOptions.IgnoreCase);
        return m.Value;

    }

使用上面的代码,我访问一个表transIndex,该表实质上是从各种用户文档中选出的单词索引.问题在于,并非所有条目实际上都是单词.小节,甚至下划线,例如___________,,也将被保存.

With the above code I access a table, transIndex that is essentially a word index of culled from various user documents. The problem is that not all entries are actually words. Nubers, and even underscore lines, such as, ___________,, are saved as well.

我只想保留我的自定义方法IsWord返回的单词(目前,我实际上还没有开发解析机制).但是,如IsWord函数所示,它将返回string.

I'd like to keep only the words that my custom method IsWord returns (at the present time I have not actually developed the parsing mechanism). But as the IsWord function shows it will return a string.

因此,使用let,我将自定义方法引入查询中并将其用作分组参数,可以在我的对象中选择.执行后,我得到了无数的东西:

So, using let I introduce my custom method into the query and use it as a grouping parameter, the is selectable into my object. Upon execution I get the omninous:

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

我还需要确保仅返回符合IsWord条件的记录.

I also need to make sure that only records that match the IsWord condition are returned.

推荐答案

这是说它不了解您的IsWord方法如何将其转换为SQL.

It is saying it does not understand your IsWord method in terms of how to translate it to SQL.

坦率地说,它并没有太大作用,为什么不将其替换为

Frankly it does not do much anyway, why not replace it with

return (from trindex in context.transIndexes
                let trueWord = trindex.word
                join trans in context.Transcripts on trindex.transLineUID equals trans.UID
                group new { trindex, trans } by new { TrueWord = trueWord, trindex.transID } into grouped
                orderby grouped.Key.word
                where grouped.Key.transID == transid
                select new WordIndexModel
                {
                    Word = TrueWord,
                    Instances = grouped.Select(test => test.trans).Distinct()
                });

EF可以将哪些方法转换为SQL,我无法列出您的列表,但是它永远无法转换您所编写的简单方法.但是它们是一些它可以理解的内置函数,例如MyArray.Contains(x),它可以将其转换为类似的

What methods can EF translate into SQL, i can't give you a list, but it can never translate a straight forward method you have written. But their are some built in ones that it understands, like MyArray.Contains(x) for example, it can turn this into something like

...
WHERE Field IN (ArrItem1,ArrItem2,ArrItem3)

如果要编写与linq兼容的方法,则需要创建EF可以理解并转化为SQL的表达式树.

If you want to write a linq compatible method then you need to create an expresion tree that EF can understand and turn into SQL.

这是让事情有点动摇的地方,但本文可能对

This is where things star to bend my mind a little but this article may help http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx.

这篇关于LINQ查询中的自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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