Linq 左连接 lambda 表达式和结果到列表
Linq left join lambda expression and result to a list
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; }
}
正在尝试编写一个 lambda 表达式,它将执行左连接和 return 列表。该列表应包含书籍,但
BookOrder 可以为空。
我试过以下导致构建错误:
Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<...BookOrder> to ..BookOrder
An explicit conversion exists (are you missing a cast?) on Line 5 (red
squigles on bko)
我无法更改图书或图书订单 类,因为这是第 3 方,即我必须满足下面列出的 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();
这是您的 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();
这里是你的 lambda 表达式版本
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();
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; }
}
正在尝试编写一个 lambda 表达式,它将执行左连接和 return 列表。该列表应包含书籍,但 BookOrder 可以为空。
我试过以下导致构建错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<...BookOrder> to ..BookOrder An explicit conversion exists (are you missing a cast?) on Line 5 (red squigles on bko)
我无法更改图书或图书订单 类,因为这是第 3 方,即我必须满足下面列出的 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();
这是您的 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();
这里是你的 lambda 表达式版本
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();