select * from books as A join (select * from Orders) as B on A.BookId = B.BookId select A.BookId,Authorname,title,Address,Num from books A right join (select * from Orders) as B on A.BookId = B.BookId
存储过程 jon exec sp_executesql N‘SELECT [Extent1].[OrderId] AS [OrderId], [Extent1].[Address] AS [Address], [Extent1].[Num] AS [Num], [Extent2].[BookId] AS [BookId], [Extent2].[AuthorName] AS [AuthorName], [Extent2].[Title] AS [Title] FROM [dbo].[Orders] AS [Extent1] INNER JOIN [dbo].[books] AS [Extent2] ON [Extent1].[BookId] = [Extent2].[BookId] WHERE [Extent1].[BookId] = @p__linq__0‘,N‘@p__linq__0 int‘,@p__linq__0=1
//使用join连接 //查询订单中的指定Bookid的数据 join 图书表中的指定bookid的数据 var data = db.Order.Where(x => x.BookId == id).Join(db.Book, s => s.BookId, j => j.BookId, (s, j) => new { Address = s.Address, Num = s.Num, OrderId = s.OrderId, BookId = j.BookId, AuthorName = j.AuthorName, Title = j.Title }); ViewBag.Data = data; return View();//根据图书id查找订单
读取数据
@foreach (var i in ViewBag.Data) { <tr> <td>@i.GetType().GetProperty("Title").GetValue(i, null)</td> <td>@i.GetType().GetProperty("AuthorName").GetValue(i, null)</td> <td>@i.GetType().GetProperty("Address").GetValue(i, null)</td> <td>@i.GetType().GetProperty("Num").GetValue(i, null)</td> <td> <a onclick="del(this)">删除</a> </td> </tr> }