如何对具有多个连接的数据进行分组? [英] How to group data having multiple joins?

查看:66
本文介绍了如何对具有多个连接的数据进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework开发基于MVC模式的ASP.Net应用程序。

I am developing an ASP.Net application based on MVC pattern using Entity Framework.

我有以下表格:


  1. Case(Id,CourtId,CaseNo)
  2. Court(Id,HeadCourtId,CourtName)
  3. HeadCourt(Id,Name)
  4. 状态(Id,CaseId,日期,处理)

我想要的是在选定的主管法院下显示已解决的案件。

What I want is to display disposed-off Cases under the selected Head Court.

>>控制器

>>Controller

public ActionResult ShowDisposed(int? HId, string hCourt)
    {
        var initialQuery = from d in db.Status.Where(x => !x.Dispose.Contains("P"))
                           select d;

        var finalQuery =

               (from p in initialQuery
                join c in db.Cases on p.CaseId equals c.Id
                join cr in db.Courts on c.ourtId equals cr.Id
                join h in db.HeadCourts on cr.HeadCourtId equals HId
                orderby p.Date descending
                select new
                {
                    CaseId = c.fldCaseId,
                    CaseNo = c.fldCaseNo,
                    Court = cr.fldName,
                    Stage = db.tblStatus.Where(x => x.fldCaseId.Equals(c.fldCaseId)).OrderByDescending(x => x.fldDate).Select(x => x.fldStage).FirstOrDefault(),

                }).ToList();
            

        var result = finalQuery.ToList().Select(t => new vmCourtPad
        {
            CaseId =t.CaseId,
            CaseNo = t.CaseNo,
            Court = t.Court,
            Stage = t.Stage

        }).ToList();

       


        ViewBag.Head = hCourt;

        return View(result);

    }

>> ViewModel

>> The ViewModel

namespace CCIS.ViewModel
{
    public class vmCourtPad
    {
       
        public string CaseNo { get; set; }
        public string Court { get; set; }
        public int CaseId { get; set; }
     
        
    }
}

上述查询完美但多次检索记录。例如,

The above query retrieves the records perfectly but multiple times. For instance,

而不是显示记录:

主审法院:最高法院


  1. 案例1
  2. 案例3

我显示如下:

主审法院:最高法院


  1. 案例1
  2. 案例1
  3. 案例1
  4. 案例1
  5. 案例1
  6. 案例3
  7. 案例3
  8. 案例3
  9. 案例3
  10. 案例3
  1. Case 1
  2. Case 1
  3. Case 1
  4. Case 1
  5. Case 1
  6. Case 3
  7. Case 3
  8. Case 3
  9. Case 3
  10. Case 3

请帮助解决问题。

问候,

Arun

推荐答案

如果您提供完整的数据结构,这会很有帮助,但我从表格中猜测Case-> Court,Court-> HeadCourt和Status-> Case之间存在一对多的关系。在这种情况下,每个案例的多行似乎是由于每个案例有多个
状态行。我猜你需要对你要包含的单个状态行进行更严格的查询,而不是db < span style ="color:#000000">状态 其中 x
=> x Dispose 包含 " P" ))
您可以通过在输出中包含一些状态属性来验证这一点。
看起来你想要最新的案例状态行没有包含P的Dispose,所以在初始查询中包含该逻辑而不是在第二个查询中包含查询。

It's helpful if you give the complete data structure, but I'm guessing from the tables that there are one-to-many relationships from Case->Court, Court->HeadCourt, and Status->Case. In that case the multiple rows per case seem to be due to multiple status rows per case. I'm guessing you need a more restrictive query for the single status row you want to include instead of db.Status.Where(x => !x.Dispose.Contains("P")). You can verify that by including some status attributes in your output. It looks like you want the most recent case status row Not having Dispose containing P, so include that logic in the initial query instead of in the lookup in the second query.


这篇关于如何对具有多个连接的数据进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆