2017-05-26 9 views
1

を使用した場合のエンティティにLINQでサポートされていませんタイプのメンバー「年齢は」我々は常に我々のアプリケーションでこのようなデータを選択しODataQueryOptions

public class DummyDto { 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public int Age {get;set;} 
} 
とすぐに我々はページングを実装するために ODataQueryOptionsを使用して、これはエラーになり

The specified type member "Age" is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

"Age"はDummyDtoのもう1つのプロパティで、上のコードでは選択していません。

我々はこのようにページングを実装:

public PageResult<DummyDto> GetAll(ODataQueryOptions<DummyDto> queryOptions) { 
    var result = queryOptions.applyTo(
       myDummyService.getAll().Where(x => x.Id > 1000).Select(x => new DummyDto() { 
         Id = x.Id, 
         Name = x.Name 
       }) 
    }); 

    return new PageResult<DummyDto>(results as IEnumerable<DummyDto>); 

我々はこれにクエリを変更したい場合は、エラーが表示されなくなります。

public PageResult<DummyDto> GetAll(ODataQueryOptions<DummyDto> queryOptions) { 
    var result = queryOptions.applyTo(
       myDummyService.getAll().Where(x => x.Id > 1000).Select(x => new DummyDto() { 
         Id = x.Id, 
         Name = x.Name, 
         Age = null // simply set the property to null, because we don't want/need it in this case 
       }) 
    }); 

誰でも何らかの理由がありますか?

+0

のようなあなたのプロパティを宣言しますか? –

+1

DummyDtoの定義が追加されました。 3つのプロパティ(またはそれ以上)を持っていますが、2つだけ必要です。 –

+0

intを使用しますか?年齢とIDを定義して、null値が許容されるようにします。 – jdweng

答えて

0

私が知る限り、queryOptions.applyTo()はクラスの各プロパティに適用を試みていますが、Ageはデータベースオブジェクトに接続していないため適用できません。

私たちはちょうどこれに新しいクラスを作成していますか?

public class DummyDto2 { 
    public int Id {get;set;} 
    public string Name {get;set;} 
} 
+0

これは可能性もあることはわかっていますが、特定のフィールドを選択するたびにカスタムdtoを作成することも意味します。これは、過度の種類の –

0

年齢はモデル内でnull値ではなく、完全にnull値を割り当てるようにしてください。エラーが発生するのはそのためです。

あなたは `DummyDto`の定義を表示することができ、このモデル

public int? Age {get;set;}