3

这是另一个“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.
4

2 回答 2

6

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

相反,您应该编写一个接受 anIQueryable并返回 another的方法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();
于 2013-10-10T16:55:58.967 回答
-1

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

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

于 2013-10-10T16:57:26.183 回答