You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在C# MVC中如何实现PhieuMuonSach与CTPhieuMuonSach表的内连接

在C# MVC中实现PhieuMuonSach与CTPhieuMuonSach内连接并关联模型用于视图展示

嘿,我来帮你搞定这个内连接的需求!从你给出的PhieuMuonSach模型来看,已经配置了和CTPhieuMuonSach的集合导航属性,这正好可以用来实现关联查询。下面分两种常用的方式来实现:

方法一:利用Entity Framework导航属性实现内连接效果

这种方式最简洁,因为EF会自动处理表之间的关联关系。我们只需要在查询时主动加载关联的CTPhieuMuonSach数据,并过滤出有对应明细的借书单(达到内连接的效果)。

在你的Controller中,编写Action方法如下:

using System.Linq;
using System.Web.Mvc;
// 替换成你的DbContext命名空间
using YourProject.Models;

public class PhieuMuonController : Controller
{
    private readonly YourDbContext db = new YourDbContext();

    public ActionResult Index()
    {
        // 使用Include加载关联的明细数据,Where条件确保只返回有明细的借书单(内连接)
        var phieuMuonWithDetails = db.PhieuMuonSach
                                     .Include(p => p.CTPhieuMuonSach)
                                     .Where(p => p.CTPhieuMuonSach.Any())
                                     .ToList();

        return View(phieuMuonWithDetails);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

方法二:手动编写LINQ内连接查询

如果你需要更灵活地控制查询字段或逻辑,可以手动写内连接的LINQ语句。如果只是要关联到PhieuMuonSach模型,其实方法一足够,但如果需要自定义返回数据,也可以创建ViewModel来存放结果。这里先给出内连接的基础写法:

var innerJoinResult = from p in db.PhieuMuonSach
                      join ct in db.CTPhieuMuonSach on p.Id_PhieuMuon equals ct.Id_PhieuMuon
                      select new 
                      {
                          PhieuMuon = p,
                          CTPhieuMuon = ct
                      };
// 如果要转换成PhieuMuonSach集合并填充明细,可以进一步处理
var phieuMuonList = innerJoinResult.GroupBy(x => x.PhieuMuon.Id_PhieuMuon)
                                   .Select(g => 
                                   {
                                       var phieuMuon = g.First().PhieuMuon;
                                       phieuMuon.CTPhieuMuonSach = g.Select(x => x.CTPhieuMuon).ToList();
                                       return phieuMuon;
                                   })
                                   .ToList();

在视图中展示关联数据

创建强类型视图,模型指定为IEnumerable<YourProject.Models.PhieuMuonSach>,然后循环展示主表和明细数据:

@model IEnumerable<YourProject.Models.PhieuMuonSach>

<h2>借阅单列表</h2>
<table class="table">
    <thead>
        <tr>
            <th>借阅单ID</th>
            <th>读者ID</th>
            <th>借书日期</th>
            <th>借阅明细</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var phieuMuon in Model)
        {
            <tr>
                <td>@phieuMuon.Id_PhieuMuon</td>
                <td>@phieuMuon.Id_DocGia</td>
                <td>@phieuMuon.NgayMuon?.ToString("yyyy-MM-dd")</td>
                <td>
                    <ul>
                        @foreach (var ct in phieuMuon.CTPhieuMuonSach)
                        {
                            <li>图书ID:@ct.Id_Sach | 数量:@ct.SoLuong</li>
                        }
                    </ul>
                </td>
            </tr>
        }
    </tbody>
</table>

注意事项

  • 确保CTPhieuMuonSach模型中配置了正确的外键和导航属性,比如:
    [Table("CTPhieuMuonSach")]
    public partial class CTPhieuMuonSach
    {
        [Key]
        public int Id_CTPhieuMuon { get; set; }
        public int Id_PhieuMuon { get; set; }
        public int Id_Sach { get; set; }
        public int SoLuong { get; set; }
    
        [ForeignKey("Id_PhieuMuon")]
        public virtual PhieuMuonSach PhieuMuonSach { get; set; }
    }
    
  • 如果使用EF Core,记得引用Microsoft.EntityFrameworkCore命名空间,Include方法的用法和EF6一致。

内容的提问来源于stack exchange,提问作者Chi Chi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:13