2013-10-10 94 views
3

这里还有另外一个“LINQ to entities不能识别方法问题”......但是,下面的代码不是基本上完全相同的东西吗?LINQ to Entities方法未识别 - 扩展方法样式

作品:

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. 

回答

6

当查询供应商认为该方法不知道该怎么办。它不能进入​​并看到方法的源代码,它可以查看对象的方式并查看完成的操作。它不能在客户端评估它,因为它还没有这些项目,并且它不能想到任何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(); 
+0

是否有可能创建一个可以在你的。选择()方法的内部执行的扩展方法,新MyModelDTO()初始化的内部?我有一些属性,我希望能够在.Select()方法内部分别执行WHERE逻辑......实质上是对相关实体执行过滤包含。 – ClearCloud8

+0

@ ClearCloud8它确实取决于你想要做什么的细节,所以你最好创建一个新的问题,其中可以包含一些你想写的东西的例子。 – Servy

相关问题