【问题标题】:Problem with Linq query .net MVCLinq 查询.net MVC 的问题
【发布时间】:2026-01-21 16:15:01
【问题描述】:

public ActionResult Performances(string id)
    {
        var query =
    from f in _db.Production
    join g in _db.Run on f.show equals g.Production.show
    join l in _db.Performance on g.startDate equals l.runStartDate
    where f.show == id
    select new ShowPerformance
    {
        Venuename = g.venue,
        Showname = f.show,
        RunStart = g.startDate,
        RunEnd = g.endDate,
        PerformanceDate = l.performanceDate,
        PerformanceTime = l.performanceTime
    };


    return View(query.ToList());


    }

查询不能区分 ShowA run1 和 Show A run2 中的表演,它只是复制 ShowA run1 和 Show A run2 中的所有表演

【问题讨论】:

  • 能否提供数据库中数据的摘录?
  • 首先,我可以建议将变量名称更改为更易于理解的名称(prod、run 和 perf 而不是 f、g 和 l)。另外,我对连接线很好奇——Production.show 是什么?
  • @HitLikeAHammer @Femaref 感​​谢您的回答。下面的答案就可以了。

标签: sql asp.net-mvc linq linq-to-entities


【解决方案1】:

我认为问题可能在于您如何将性能加入到运行/生产中

var query =
        from f in _db.Production
        join g in _db.Run on new {f.show, f.year} equals new {g.show, g.year}
        join l in _db.Performance on new {g.venue, g.startDate} equals new {l.venue, l.runStartDate}
        where f.show == id 
        select new ShowPerformance
        {
            Venuename = g.venue,
            Showname = f.show,
            RunStart = g.startDate,
            RunEnd = g.endDate,
            PerformanceDate = l.performanceDate,
            PerformanceTime = l.performanceTime
        };

如果没有 on g.runId equals l.runId 这样的东西,那么您将获得所有制作/运行的所有表演。

【讨论】:

  • @Stuart 感谢查询效果很好,只获得特定节目的表演。
  • @Stuart 我已经添加了相同产品的另一个运行,并且查询不区分运行并拉动两次运行的所有性能。如果可以提供一些帮助,我已经用我的 er 图表的图片更新了这个问题。
  • 性能和运行是如何结合在一起的?看起来您需要使用 where g.venue == l.venue && g.startDate <= l.performanceDate && g.endDate >= l.performanceDate 之类的东西进行相当复杂的 CROSS JOIN - 坐下来想想 - 你知道这个数据库 - 我们不知道!
  • 我还担心“年”是数据库中的一个字段 - 这肯定是日期的一部分吗?
  • 在这种情况下,将这两个字段都放入连接表达式中 - 见上文