在Linq to Entities中使用扩展方法 [英] Using extension method in Linq to Entities

查看:70
本文介绍了在Linq to Entities中使用扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

public static IEnumerable<TableRowModel> GetRows()
        {
            var to_ret = db.TableRows.select(x=> new TableRowModel(){
                TableRowId = x.TableRowId,
                Type = x.Type,
                Name = x.Name,
                CreatedAt = x.CreatedAt,
                ModifiedAt = x.ModifiedAt,
                Enums = x.Enums.Select(y => y.ToEnumModel()),
            });
            return to_ret;
        }

public static EnumModel ToEnumModel(this Enum x)
        {
            var to_ret = new EnumModel()
            {
                CFPId = x.CFPId,
                CreatedAt = x.CreatedAt,
                ModifiedAt = x.ModifiedAt,
            };
            return to_ret;
        }

使用GetRows方法时出现以下错误:

I get the following error when using the GetRows method:

LINQ to Entities无法识别该方法

LINQ to Entities does not recognize the method

鉴于该错误,可以理解LINQ To Entities无法识别扩展方法ToEnumModel. 我想知道是否有办法解决? 这样我就不会在GetRows扩展名中再次重复ToEnumModel代码.

Given the error, it's understood that LINQ To Entities is not able to recognize the extension method ToEnumModel. I would like to know if there is a way around this? So that I would not be repeating ToEnumModel code again in GetRows extension.

推荐答案

在正常情况下,当在EntityFramework中执行类似Where的操作时,它实际上并没有像对可枚举的操作(例如一个列表).而是将其转换为SQL,然后由Db提供程序执行.这意味着不能执行某些操作,例如对对象使用扩展方法,因为转换器无法将这些操作转换为SQL.

Under normal circumstances, when performing an operation like a Where in EntityFramework, it isn't actually executed in memory like when you operate on an enumerable (like a List). Instead, it converted into SQL which is then executed by the Db provider. This means that doing certain things, such as using extension methods on objects, is not an option, as the converter cannot turn such things into SQL.

最快,最简单的解决方案是将您的设置加载到内存中,并可以像其他任何枚举一样对其进行操作.您可以这样操作:

The quickest and easiest solution would be to load your set in-memory, where it can be operated on like any other enumerable. You can do this like so:

var result = myDbSet.AsEnumerable().Etc()

Etc()此处表示您要运行的所有其他操作.但是请注意,这会将所有数据加载到内存中,这在某些情况下可能会非常缓慢且昂贵.缓解此问题的一种方法是在使用扩展方法之前将AsEnumerable() 正确放在首位,从而将尽可能多的操作分担给提供程序.例如:

Etc() here represents all other operations you want to run. Be advised however, that this will load all data into memory, which may be prohibitively slow and expensive in some scenarios. One way to alleviate this is to put AsEnumerable() right before you need to use the extension method, thus offloading as many operations as possible to the provider. For example this:

var result = myDbSet.Where([condition]).AsEnumerable().Select(item => item.ExtensionMethod());

可能比这更快,更轻巧:

Might be much quicker and lighter than this:

var result = myDbSet.AsEnumerable().Where([condition]).Select(item => item.ExtensionMethod());

这篇关于在Linq to Entities中使用扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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