c# - 获取自联接表的不同类型值的问题
问题描述
我有一个人口自联接表。人口在村一级输入,应在联合委员会 (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
解决方案
您需要加入两个课程:
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; }
}
}
推荐阅读
- hook-woocommerce - 根据结帐页面上的选择,在 woocommerce 中以编程方式覆盖产品价格
- spring-boot - 我无法将收到的来自电报机器人的照片保存到像字节数组这样的数据库中
- javascript - 如何在不删除导航栏的情况下创建页面?
- python - win32com 以外的替代方法
- python - ValueError:DataFrame 的真值不明确 - 用于填充空数组的简单 for 循环不起作用
- java - 持久分离对象出现错误“在同步期间,通过未标记为级联 PERSIST 的关系找到了一个新对象”
- testing - 我想知道是否可以指定要创建诱惑报告文件夹的路径
- javascript - 为什么 a = {b:4}[r,s,e]= ['Ronaldo', 'Messi', 'Aguero'] 会给出 ['Ronaldo', 'Messi', 'Aguero'] 作为输出?
- jquery - 尝试更改 CSS 时,GrapesJS 导入选项不起作用
- woocommerce - 在 woocommerce 上特定目录的单个产品简短描述下添加文本