Asp.Net MVC 5汇总来自多个表的数据

时间:2018-11-02 00:02:58

标签: c# asp.net-mvc linq asp.net-mvc-5 linq-to-sql

我正在尝试创建一个视图模型,该视图模型汇总应用程序中每个公司的信息。不幸的是,对于我的一生,我无法获得所需的结果……我看到了示例和其他问题,它们很有意义,但是当我不得不编写自己的解决方案的那一刻,一切都崩溃了。

无论如何,举例来说,我想计算每个公司有多少个分支机构或公司所处的位置。这两者都是相似的,但是它们会影响我的架构中的不同表。

关于实际表关系,以下是模型:

public class Branch
{

    [Key]
    public int BranchId { get; set; }

    [Required]
    [Display(Name = "Branch")]
    public string BranchName { get; set; }

    [Required]
    public int CompanyId { get; set; }

    [Required]
    public int LocationId { get; set; }

    public virtual Company Company { get; set; }

    public virtual Location Location { get; set; }
}

public class Location
{
    [Key]
    public int LocationId { get; set; }

    [Required]
    [Display(Name = "Location")]
    public string LocationName { get; set; }

    public ICollection<Branch> Branches { get; set; }
}

public class Company
{
    [Key]
    public int CompanyId { get; set; }

    [Display(Name = "Company")]
    [Required]
    public string CompanyName { get; set; }

    public ICollection<Branch> Branches { get; set; }
}

ViewModel代码:

public class CompaniesRegisterViewModel
{
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
    public int BranchId { get; set; }
    public string BranchName { get; set; }
    public int BranchCount { get; set; }
    public int LocationId { get; set; }
    public string LocationName { get; set; }
    public int LocationCount { get; set; }

}

我当前的(不完整的)控制器代码

    public ActionResult CompaniesRegisterViewModel()
    {
        var currentUser = User.Identity.GetUserId();

            var companyList = _db.Companies.ToList();

        var branchList = _db.Branches.Where(x => x.Company.CompanyId == );
        int count = 0;
        foreach (var branch in branchList)
            count++;
        var personVmList = companyList.Select(x =>
                new CompaniesRegisterViewModel
                {
                    CompanyName = x.CompanyName,
                    CompanyId = x.CompanyId,
                    BranchCount = count,
                    LocationCount = _db.Locations.Count(x => x.LocationId == )
                    //BranchId = x.BranchId,
                    //BranchName = x.Branch.BranchName
                }).ToList();

            return View(personVmList);
        //}

        //return View();
    }

如上所述,我试图以某种方式获取并在该列表索引中传递公司的ID。我肯定做错了,因为到目前为止我没有尝试过。

from loc in Locations
join br in Branches on loc.LocationId equals br.LocationId
select loc.LocationName

上面的LINQ查询是一个好的方向吗?


更新#1

public ActionResult CompaniesRegisterViewModel()
    {
        var companyList = _db.Companies.ToList();
        var companyList1 = _db.Companies.GroupBy(x => x.CompanyId);

        //PersonInCompanyViewModel personInCompanyVM = new PersonInCompanyViewModel();

        var test = _db.Companies.GroupBy(info => info.CompanyId)
            .Select(group => new
            {
                CompanyId = group.Key,
                Count = group.Select(x => x.Branches).Count()
            }).OrderBy(x => x.CompanyId);

        var branches = _db.Branches.GroupBy(info => info.BranchId)
            .Select(group => new
            {
                BranchId = group.Key,
                Count = group.Count()
            }).OrderBy(x => x.BranchId);

        var test2 = branches.AsQueryable().Select(x =>
            new CompaniesRegisterViewModel
            {
                BranchCount = x.Count

            });

        var branchlist = _db.Branches.ToList();

        var branchCount =
            from p in companyList
            group p by p.Branches into g
            select new { Branch = g.Key, BranchCount = g.Count() };

        var personVmList = companyList.Select(x =>
                new CompaniesRegisterViewModel
                {
                    CompanyName = x.CompanyName,
                    CompanyId = x.CompanyId,
                    BranchCount = branchCount.Count()
                    //BranchCount = _db.Companies.GroupBy(b => b.CompanyId).Count(1, d => d.Branches.Count)
                    //LocationCount = companyList1.Count()
                    //BranchId = x.BranchId,
                    //BranchName = x.Branch.BranchName
                }).ToList();

            return View(personVmList);
        //}

        //return View();
    }

1 个答案:

答案 0 :(得分:0)

万一有人偶然发现了这个问题,这就是我需要的答案。

var model = _db.Branches.GroupBy(x => new { ID = x.CompanyId, Name = x.Company.CompanyName }).Select(y => new CompaniesRegisterViewModel { CompanyId = y.Key.ID, CompanyName = y.Key.Name, BranchCount = y.Count() });

感谢@Stephen Muecke!