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

时间:2019-05-11 20:01:05

标签: c# asp.net-mvc entity-framework linq

我有一个自我加入人口表。人口是在村庄一级输入的,应该在联盟理事会(UC),Tehsil和地区一级自动计算。

我在此应用程序中使用.net MVC。以下是我的代码

人口类型枚举

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

人口结构,在此处添加了村庄,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; }
    }

输入村庄一级的人口

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

2 个答案:

答案 0 :(得分:0)

您需要加入这两个课程:

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

答案 1 :(得分:0)

这是问题的答案


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