【问题标题】:LINQ to Entities Method Not Recognized - Extension Method Style无法识别 LINQ to Entities 方法 - 扩展方法样式
【发布时间】:2013-10-10 16:30:53
【问题描述】:

这里还有另一个“LINQ to entity 无法识别方法问题”……但是,下面的代码不是在做同样的事情吗?

作品:

var returnData = from x in MyEntities.MyDBSet
                        where x.MyDBSetPrimaryKey == id
                        select new Models.MyModelDTO
                        {
                            MyPropOne = (int)x.MyModel.MyOtherPropOne,
                            MyPropTwo = x.MyOtherPropTwo ?? 0,
                            MyPropThree = x.MyModel.MyOtherPropThree,
                            MyPropFour = x.MyModel.MyOtherPropFour,
                            MyPropFive = x.MyModel.Entity.MyOtherPropFive,
                            MyPropSix = x.MyModel.MyOtherPropSix == null ? 0 : (decimal)x.MyModel.MyOtherPropSix,
                            MyPropSeven = x.MyModel.SomeType.MyOtherPropSeven,
                            MyPropEight = (int)x.MyModel.MyOtherPropEight,
                            MyPropNine = x.MyModel.MyPropNine == null ? 0 : (int)x.MyModel.MyOtherPropNine,
                            MyPropTen = x.MyModel.MyOtherPropTen == null ? 0 : (int)x.MyModel.MyOtherPropTen,
                            MyPropEleven = x.OtherEntity.MyOtherPropEleven,
                            MyPropTwelve = x.MyOtherpropTwelve
                        };

不起作用:

包装在扩展方法中的完全相同的赋值:

public static MyModelDTO ToModelDTO(this MyModel x)
    {
        return new MyModelDTO()
        {
            MyPropOne = (int) x.MyModel.MyOtherPropOne,
            MyPropTwo = x.MyOtherPropTwo ?? 0,
            MyPropThree = x.MyModel.MyOtherPropThree,
            MyPropFour = x.MyModel.MyOtherPropFour,
            MyPropFive = x.MyModel.Entity.MyOtherPropFive,
            MyPropSix = x.MyModel.MyOtherPropSix == null ? 0 : (decimal) x.MyModel.MyOtherPropSix,
            MyPropSeven = x.MyModel.SomeType.MyOtherPropSeven,
            MyPropEight = (int) x.MyModel.MyOtherPropEight,
            MyPropNine = x.MyModel.MyPropNine == null ? 0 : (int) x.MyModel.MyOtherPropNine,
            MyPropTen = x.MyModel.MyOtherPropTen == null ? 0 : (int) x.MyModel.MyOtherPropTen,
            MyPropEleven = x.OtherEntity.MyOtherPropEleven,
            MyPropTwelve = x.MyOtherpropTwelve
        };
    }

后来又叫:

var returnData = from x in MyEntities.MyDBSet
                        where x.MyDBSetPrimaryKey == id
                        select x.ToModelDto();

导致:

LINQ to Entities does not recognize the method 'MyExtensionMethods.MyModels.MyModelDTO ToModelDTO(API.Models.MyModel)' method, and this method cannot be translated into a store expression.

【问题讨论】:

    标签: c# linq entity-framework linq-to-entities


    【解决方案1】:

    当查询提供者看到该方法时,它不知道如何处理它。它不能进去查看方法的源代码,它可以查看Expression对象的方式,看看做了什么。它无法在客户端对其进行评估,因为它还没有项目,并且它想不出任何 SQL 来将该方法调用转换为。

    相反,您应该编写一个接受 IQueryable 并返回另一个 IQueryable 的方法,如下所示:

    public static IQueryable<MyModelDTO> ToModelDTO(this IQueryable<MyModel> query)
    {
        return query.Select(x => new MyModelDTO()
        {
            MyPropOne = (int)x.MyModel.MyOtherPropOne,
            MyPropTwo = x.MyOtherPropTwo ?? 0,
            MyPropThree = x.MyModel.MyOtherPropThree,
            MyPropFour = x.MyModel.MyOtherPropFour,
            MyPropFive = x.MyModel.Entity.MyOtherPropFive,
            MyPropSix = x.MyModel.MyOtherPropSix == null ? 0 : (decimal)x.MyModel.MyOtherPropSix,
            MyPropSeven = x.MyModel.SomeType.MyOtherPropSeven,
            MyPropEight = (int)x.MyModel.MyOtherPropEight,
            MyPropNine = x.MyModel.MyPropNine == null ? 0 : (int)x.MyModel.MyOtherPropNine,
            MyPropTen = x.MyModel.MyOtherPropTen == null ? 0 : (int)x.MyModel.MyOtherPropTen,
            MyPropEleven = x.OtherEntity.MyOtherPropEleven,
            MyPropTwelve = x.MyOtherpropTwelve
        });
    }
    

    这里的映射仍然被编译成查询提供程序可以解析的Expression。现在你可以这样做了:

    var returnData = (from x in MyEntities.MyDBSet
                      where x.MyDBSetPrimaryKey == id
                      select x)
                      .ToModelDTO();
    

    【讨论】:

    • 是否可以在新的 MyModelDTO() 初始化程序中创建可以在 .Select() 方法内部执行的扩展方法?我有一些属性,我希望能够在 .Select() 方法内部单独执行 WHERE 逻辑......基本上对相关实体执行过滤包含。
    • @ClearCloud8 这真的取决于你想做什么的细节,所以你最好创建一个新问题,其中可以包含一些你想写的例子.
    【解决方案2】:

    您的问题和可能的解决方案与许多其他“LINQ to entity 无法识别方法”问题并没有什么不同。

    例如,请参阅https://stackoverflow.com/a/7259649/120955https://stackoverflow.com/a/18901609/120955

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 2012-12-22
      • 2014-08-09
      • 1970-01-01
      • 2012-04-03
      • 2021-11-06
      • 2012-05-03
      相关资源
      最近更新 更多