简体   繁体   English

问题得到不同类型的自我联接表的价值

[英]Issue getting different types value of self join table

I have a self join table of population. 我有一个自加入人口表。 Population is entered at village level and it should be automatically calculated on Union council(UC), Tehsil and District Level. 人口是在村庄一级输入的,应该在联盟理事会(UC),Tehsil和地区一级自动计算。

I am using .net MVC in this application. 我在此应用程序中使用.net MVC。 Following is my code 以下是我的代码

enum of population type 人口类型的枚举

 public enum UnitType
    {
        Village,
        UC,
        Tehsil,
        Dist
    }

population structure, here names of villages, UC, Tehsil and district are added 人口结构,这里添加了村庄,UC,Tehsil和地区的名称

public class Village
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public UnitType UnitType { get; set; }
        public int? ParientId { get; set; }
        public Village Parient { get; set; }
    }

Enter population at village level 输入村庄一级的人口

public class Population
    {
        public int Id { get; set; }
        public int VillageId { get; set; }
        public Village Village { get; set; }

        public int NoOfPerson { get; set; }
    }


I need the following output result. I can get the village level population but i am confused in getting related totals. Its looks very simple but i think i am not going in right direction.

    POPULATION                  
Code    Name    Type           Population           
1   Chakwal Disttrict   20000   (total population of all tehsils)       
2   Choa    Tehsil          20000   (Tehsil total of two Union Councils)        
3   Dalwal  UC          14300   UC is total of village population       
4   Waulah  Village          9800           
5   DalPur  VIllage          4500           
    Dulmial UC           5700   UC is total of village population       
    Tatral  Village          3400           
    Arar    Village          2300

You need to join the two classes : 您需要加入两个课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {

            List<Village> villages = new List<Village>() {
                new Village() { Id = 1, Name = "Chakwal", UnitType = UnitType.Dist},
                new Village() { Id = 2, Name = "Choa", UnitType = UnitType.Tehsil},
                new Village() { Id = 3, Name = "Dalwal", UnitType = UnitType.UC},
                new Village() { Id = 4, Name = "Waulah", UnitType = UnitType.Village},
                new Village() { Id = 5, Name = "DalPur", UnitType = UnitType.Village},
                new Village() { Id = 6, Name = "Dulmial", UnitType = UnitType.UC},
                new Village() { Id = 7, Name = "Tatral", UnitType = UnitType.Village},
                new Village() { Id = 8, Name = "Arar", UnitType = UnitType.Village}
            };
            List<Population> populations = new List<Population>() {
                new Population() { Id = 1, NoOfPerson = 20000},
                new Population() { Id = 2, NoOfPerson = 20000},
                new Population() { Id = 3, NoOfPerson = 14300},
                new Population() { Id = 4, NoOfPerson = 9800},
                new Population() { Id = 5, NoOfPerson = 4500},
                new Population() { Id = 6, NoOfPerson = 5700},
                new Population() { Id = 7, NoOfPerson = 3400},
                new Population() { Id = 8, NoOfPerson = 2300}
            };

            var results = (from v in villages
                           join p in populations on v.Id equals p.Id
                           select new { v = v, p = p }
                          ).ToList();
            StreamWriter writer = new StreamWriter(FILENAME);

            writer.WriteLine("{0,25}","POPULATION");
            writer.WriteLine("{0,-5}{1,-8}{2,-14}{3,-10}", "Code", "Name", "Type", "Population");

            foreach (var result in results)
            {
                writer.WriteLine("{0,-5}{1,-8}{2,-14}{3,-10}", result.v.Id.ToString(), result.v.Name, result.v.UnitType.ToString(), result.p.NoOfPerson.ToString());
            }
            writer.Flush();
            writer.Close();

        }
    }
    public enum UnitType
    {
        Village,
        UC,
        Tehsil,
        Dist
    }
    public class Village
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public UnitType UnitType { get; set; }
        public int? ParientId { get; set; }
        public Village Parient { get; set; }
    }
    public class Population
    {
        public int Id { get; set; }
        public int VillageId { get; set; }
        public Village Village { get; set; }

        public int NoOfPerson { get; set; }
    }
}

Here is the answer for the question 这是问题的答案


var villages = db.Populations.Include(l => l.Village).ToList();

            var ucpop = villages.GroupBy(l => l.UCId).Select(g=> new {
                ucId = g.Key,                
                UcName = db.Villages.Find(g.Key),             
                VillageCount = g.Count(),
                UCPop = g.Sum(l=>l.NoOfPerson),
                villages = g.Where(l=>l.Village.UnitType == UnitType.Village).ToList()
            }).ToList();

            var tehpop = ucpop.GroupBy(l => l.UcName.ParientId).Select(g => new
            {
                tehId = g.Key,
                tehName = db.Villages.Find(g.Key),
                tehCount = g.Count(),
                tehPop = g.Sum(l => l.UCPop),
                uclist = g.Where(l=>l.UcName.UnitType == UnitType.UC).ToList()
            }).ToList();

            var distpop = tehpop.GroupBy(l => l.tehName.ParientId).Select(g => new
            {
                distId = g.Key,
                distName = db.Villages.Find(g.Key),
                distCount = g.Count(),
                distPop = g.Sum(l => l.tehPop),
                tehlist = g.Where(l => l.tehName.UnitType == UnitType.Tehsil).ToList()
            }).ToList();

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

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