首页 > 解决方案 > 获取自联接表的不同类型值的问题

问题描述

我有一个人口自联接表。人口在村一级输入,应在联合委员会 (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

标签: c#asp.net-mvcentity-frameworklinq

解决方案


您需要加入两个课程:

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

推荐阅读