3

さらに別の「エンティティへのLINQはメソッドの質問を認識しません」...しかし、以下のコードは基本的にまったく同じことをしていませんか?

作品:

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 を考えることもできません。

代わりに、次のように、を受け取り、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();
于 2013-10-10T16:55:58.967 に答える