2017-05-10 12 views
0

私はルーキーですが、asp.netについてはasp.netでrestfull APIを作成しようとしています。以下のコードでSaldoとVerenigingからデータを取得しようとしています。は内部結合のタイプsystem.linq.iqueryableを暗黙的に変換することはできません

saldoテーブルが含まれています

-saldoId -VerenigingId -LidId -Bedrag

ヴェレニギングテーブルが含まれています。何イム

VerenigingId ナーム locatie facebookgroupId

作成しようとしているのは、return文がすべてのVerenigingenは1票から。

public class LibraryRepository : ILibraryRepository 
{ 
    private LibraryContext _context; 

    public LibraryRepository(LibraryContext context) 
    { 
     _context = context; 
    } 

    public bool Save() 
    { 
     return (_context.SaveChanges() >= 0); 
    } 

    public IEnumerable<Vereniging> GetVerenigingenperLid(int lidId) 
    { 

     return _context.Vereniging // your starting point - table in the "from" statement 
        .Join(_context.Saldo, // the source table of the inner join 
        a => a.verenigingId,  // Select the primary key (the first part of the "on" clause in an sql "join" statement) 
        b => b.verenigingId, // Select the foreign key (the second part of the "on" clause) 
        (a, b) => new { Vereniging = a, Saldo = b }) // selection 
        .Where(c => c.Saldo.lidId == lidId); 

    } 

}

私はエラーを取得:Library.API.Entities.Verenigingヴェレニギング、Library.API.Entities: は、暗黙のうちにCONVERタイプ「system.linq.IQueryable "< <" 匿名型はできません。 Saldo Saldo >> to 'System.Collections.Generic.IEnumerable "<" Library.API.Entities.Vereniging>。明示的な変換が存在します(キャストがありませんか?)。

私はこの問題を理解しています。返り値はSaldoとPere Verenigingからなるからです。

しかし、私はこれを修正する方法を知っていません。

答えて

2

戻り値の型は、投影の型と一致する必要があります。以下の図を参照してください。

non-matching return type
クエリの最後にSelect(...)を追加:あなたが選択で(a, b) => new { Vereniging = a, Saldo = b }を返している間GetVerenigingenperLid

// ... 
    .Where(c => c.Saldo.lidId == lidId) 
    // for example 
    .Select(c => c.Vereniging); 
0

戻り値の型は、IEnumerable<Vereniging>です。

あなたはプロパティとして ヴェレニギングSaldoが含まれていますどの のViewModelを言う新しいのviewmodelクラスを宣言する必要があり、これを修正するには。

public class ViewModel 
{ 
    public Vereniging {get;set;} 
    public Saldo {get;set;} 
} 

public IEnumerable<ViewModel> GetVerenigingenperLid(int lidId) 
{ 

    return _context.Vereniging // your starting point - table in the "from" statement 
       .Join(_context.Saldo, // the source table of the inner join 
       a => a.verenigingId,  // Select the primary key (the first part of the "on" clause in an sql "join" statement) 
       b => b.verenigingId, // Select the foreign key (the second part of the "on" clause) 
       (a, b) => new ViewModel { Vereniging = a, Saldo = b }) // selection 
       .Where(c => c.Saldo.lidId == lidId); 

} 

は、それが

を役に立てば幸い
関連する問題