简体   繁体   中英

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:

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. 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?


UPDATE #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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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