2016-04-26 4 views
1
public class JoinModel 
{ 
     public Book Book { get; set; } 
     public BookOrder BookOrder { get; set; } 
} 

public class Book 
{ 
     public int BookID { get; set; } 
     public string UniqueID{ get; set; } 
     public int Year { get; set; } 
     public int BookNumber { get; set; } 
     public int Value { get; set; } 

} 

public class BookOrder 
{ 
     public int BookOrderID { get; set; } 
     public string UniqueID{ get; set; } 
     public int Year { get; set; } 
     public int BookNumber { get; set; } 
     public DateTime OrderDate { get; set; } 
} 

左結合を行いリストを返すラムダ式を書こうとしました。リストにはBooksが含まれている必要がありますが、 BookOrderはnullでもかまいません。私はビルドエラーになり、次の試してみましたLinqはリストにLambda式と結果を残しました。

が暗黙のうちに..BookOrder にタイプ 「System.Collections.Generic.IEnumerable < ... BookOrder>を変換できません明示的な変換が存在する(していますあなたはキャストを行方不明?)ライン5(BKOの赤い squigles)に

は、私は、これは、サードパーティであるとして、書籍やBookOrderクラスを変更することはできませんよ、つまり私は、下記の3つの条件に参加する必要があり。

List<JoinModel> lstJoinModel = new List<JoinModel>(); 

Line 1 - lstJoinModel = Context.Books 
Line 2 - .GroupJoin(Context.BookOrder, 
Line 3 - bk => new {  bk.UniqueID, bk.Year, bk.PostingId }, 
Line 4 - bko => new {  bko.UniqueID, bko.Year, bko.BookNumber }, 
Line 5 - (bk, bko) => new  JoinModel { Book = bk, BookOrder = bko }) 
Line 6 - .Where(r => r.Book.Value >  0).ToList(); 
+0

で行くあなたは 'JoinModel'を変更できますか? – juharr

答えて

1

ここにあなたのLINQです:

List<JoinModel> lstJoinModel = (from bk in Context.Books 
           join bko in Context.BookOrder on new { bk.UniqueID, bk.Year, bk.BookNumber } equals new { bko.UniqueID, bko.Year, bko.BookNumber } 
           into bd 
           from bd2 in bd.DefaultIfEmpty() 
           where bk.Value > 0 
           select new JoinModel { Book = bk, BookOrder = bd2 } 
           ).ToList(); 

そして、ここでは、あなたのラムダ式のバージョン

List<JoinModel> lstJoinModel = Context.Books.GroupJoin(Context.BookOrder, 
           bk => new { bk.UniqueID, bk.Year, bk.BookNumber }, 
           bko => new { bko.UniqueID, bko.Year, bko.BookNumber }, 
           (x, y) => new { Book = x, BookOrder = y }) 
           .SelectMany(x => x.BookOrder.DefaultIfEmpty(), 
           (x, y) => new JoinModel 
           { 
            Book = x.Book, 
            BookOrder = y 
           }) 
           .Where(r => r.Book.Value > 0).ToList(); 
関連する問題