简体   繁体   English

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

[英]Asp.Net MVC 5 Summarize data from multiple tables

I am trying to create a viewmodel that summarizes information for each company in my application. 我正在尝试创建一个视图模型,该模型概述我应用程序中每个公司的信息。 Unfortunately for the life of me I cannot get the the results I need... I see examples and other questions and they make sense, but the moment I have to write my own solution it all just falls apart. 不幸的是,对于我的一生,我无法获得所需的结果……我看到了示例和其他问题,它们很有意义,但是当我不得不编写自己的解决方案的那一刻,一切都崩溃了。

Anyway, as an example I would like to count how many branches each company has, or how many locations the company is based in. Both of these are sort of similar, but they affect different tables in my schema. 无论如何,作为示例,我想计算每个公司有多少个分支机构,或公司所位于的位置。这两者都是相似的,但是它们会影响架构中的不同表。

In terms of the actual table relationships, here are the models: 根据实际的表关系,以下是模型:

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 Code: 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; }

}

My current (uncomplete) controller code 我当前(不完整)的控制器代码

    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();
    }

As seen above I am trying to somehow obtain and pass the ID of the Company in that list index. 如上所示,我试图以某种方式获取该列表索引中的公司ID并通过该ID。 I must be doing it totally wrong as nothing I've tried so far worked. 我做错了,因为到目前为止我没有尝试过。

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

Is the above LINQ query a good direction? 上面的LINQ查询是一个好的方向吗?


UPDATE #1 更新#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();
    }

In case someone stumbles upon this question, here's the answer I needed. 如果有人偶然发现了这个问题,这就是我需要的答案。

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() });

Thanks @Stephen Muecke! 感谢@Stephen Muecke!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM