>

인구 집단이 있습니다. 인구는 마을 수준에서 입력되며 연합 협의회 (UC), 테 헤실 및 지구 수준에서 자동으로 계산되어야합니다.

이 응용 프로그램에서 .net MVC를 사용하고 있습니다. 다음은 내 코드입니다

인구 유형의 열거

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

인구 구조, 여기에 마을 이름, UC, 테 헤실 및 지역이 추가됨

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


  • 답변 # 1

    두 클래스에 참여해야합니다 :

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

  • 답변 # 2

    질문에 대한 답변은 다음과 같습니다

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

관련 자료

  • 이전 swift - 인증 후 iOS에서 DynamoDB 사용
  • 다음 힘내 로그 --stat 삭제 된 폴더 표시