C# 如何水平显示数据

C# 如何水平显示数据,c#,sql,sql-server,C#,Sql,Sql Server,我有四个表(员工、津贴、扣除额和Ajenda)。这些是主表,当我保存员工财务详细信息时,我将它们保存在不同的表中,如图所示。 我希望以水平方式显示员工详细信息和财务详细信息。 我正在使用C#和SQLServer2012。 第一个表包含员工主要详细信息,第二个表包含员工id和其他表 在有人再次关闭之前,这里是我的代码。我决定不使用group,因为会有很多左侧外部联接,这会使解决方案复杂化。我决定只创建一个表,然后将数据直接添加到结果表中 using System; using System.Col

我有四个表(员工、津贴、扣除额和Ajenda)。这些是主表,当我保存员工财务详细信息时,我将它们保存在不同的表中,如图所示。 我希望以水平方式显示员工详细信息和财务详细信息。 我正在使用C#和SQLServer2012。 第一个表包含员工主要详细信息,第二个表包含员工id和其他表


在有人再次关闭之前,这里是我的代码。我决定不使用group,因为会有很多左侧外部联接,这会使解决方案复杂化。我决定只创建一个表,然后将数据直接添加到结果表中

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable employees = new DataTable();
            employees.Columns.Add("empID", typeof(int));
            employees.Columns.Add("emp_name", typeof(string));
            employees.Columns.Add("emp_tele", typeof(string));

            employees.Rows.Add(new object[] { 1, "David", "025896325" });
            employees.Rows.Add(new object[] { 2, "John", "856985658" });
            employees.Rows.Add(new object[] { 3, "Micheal", "654687887" });

            DataTable allowances = new DataTable();
            allowances.Columns.Add("empID", typeof(int));
            allowances.Columns.Add("allow_name", typeof(string));
            allowances.Columns.Add("allow_Amount", typeof(int));

            allowances.Rows.Add(new object[] { 1, "allow1", 100 });
            allowances.Rows.Add(new object[] { 1, "allow2", 200 });
            allowances.Rows.Add(new object[] { 1, "allow3", 300 });
            allowances.Rows.Add(new object[] { 2, "allow1", 100 });
            allowances.Rows.Add(new object[] { 2, "allow2", 200 });

            DataTable deductions = new DataTable();
            deductions.Columns.Add("empID", typeof(int));
            deductions.Columns.Add("Dedu_name", typeof(string));
            deductions.Columns.Add("Dedu_Amount", typeof(int));

            deductions.Rows.Add(new object[] { 1, "ded1", 10 });
            deductions.Rows.Add(new object[] { 1, "ded2", 5 });
            deductions.Rows.Add(new object[] { 2, "ded1", 10 });

            DataTable ajenda = new DataTable();
            ajenda.Columns.Add("empID", typeof(int));
            ajenda.Columns.Add("ajenda_name", typeof(string));
            ajenda.Columns.Add("ajenda_Amount", typeof(int));

            ajenda.Rows.Add(new object[] { 1, "aj1", 200 });
            ajenda.Rows.Add(new object[] { 1, "aj1", 200 });
            ajenda.Rows.Add(new object[] { 1, "aj2", 300 });

            DataTable results = employees.Clone();

            string[] allow_names = allowances.AsEnumerable().Select(x => x.Field<string>("allow_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in allow_names)
            {
                results.Columns.Add(name, typeof(string));
            }


            string[] dedu_names = deductions.AsEnumerable().Select(x => x.Field<string>("Dedu_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in dedu_names)
            {
                results.Columns.Add(name, typeof(string));
            }

            string[] ajenda_names = ajenda.AsEnumerable().Select(x => x.Field<string>("ajenda_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in ajenda_names)
            {
                results.Columns.Add(name, typeof(string));
            }

            //add employees to result table
            foreach(DataRow row in employees.AsEnumerable())
            {
                results.Rows.Add(row.ItemArray);
            }

            var groupAllownaces = allowances.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupAllownaces)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("allow_name")] = row.Field<int>("allow_Amount");
                }
            }

            var groupDeductions = deductions.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupDeductions)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("Dedu_name")] = row.Field<int>("Dedu_Amount");
                }
            }

            var groupAjenda = ajenda.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupAjenda)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("ajenda_name")] = row.Field<int>("ajenda_Amount");
                 }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable employees=新DataTable();
employees.Columns.Add(“empID”,typeof(int));
employees.Columns.Add(“emp_name”,typeof(string));
employees.Columns.Add(“emp_tele”,typeof(string));
Add(新对象[]{1,“David”,“025896325”});
Add(新对象[]{2,“John”,“856985658”});
Add(新对象[]{3,“Micheal”,“654687887”});
数据表容差=新数据表();
添加(“empID”,类型(int));
添加(“允许名称”,类型(字符串));
津贴。列。添加(“允许金额”,类型(int));
添加(新对象[]{1,“allow1”,100});
添加(新对象[]{1,“allow2”,200});
添加(新对象[]{1,“allow3”,300});
添加(新对象[]{2,“allow1”,100});
添加(新对象[]{2,“allow2”,200});
数据表扣减=新数据表();
扣减。列。添加(“empID”,typeof(int));
扣减。列。添加(“扣减名称”,类型(字符串));
扣减。列。加(“扣减金额”,类型(int));
扣减。行。添加(新对象[]{1,“ded1”,10});
扣减。行。添加(新对象[]{1,“ded2”,5});
扣减。行。添加(新对象[]{2,“ded1”,10});
DataTable ajenda=新DataTable();
Add(“empID”,typeof(int));
Add(“ajenda_name”,typeof(string));
添加(“ajenda_金额”,typeof(int));
Add(新对象[]{1,“aj1”,200});
Add(新对象[]{1,“aj1”,200});
Add(新对象[]{1,“aj2”,300});
DataTable结果=employees.Clone();
string[]allow_name=accounters.AsEnumerable().Select(x=>x.Field(“allow_name”)).Distinct().OrderBy(x=>x.ToArray();
foreach(允许_名称中的字符串名称)
{
添加(名称、类型(字符串));
}
string[]dedu_name=deductions.AsEnumerable().Select(x=>x.Field(“dedu_name”)).Distinct().OrderBy(x=>x.ToArray();
foreach(dedu_名称中的字符串名称)
{
添加(名称、类型(字符串));
}
字符串[]ajenda_name=ajenda.AsEnumerable().Select(x=>x.Field(“ajenda_name”)).Distinct().OrderBy(x=>x.ToArray();
foreach(ajenda_名称中的字符串名称)
{
添加(名称、类型(字符串));
}
//将员工添加到结果表
foreach(employees.AsEnumerable()中的DataRow行)
{
results.Rows.Add(row.ItemArray);
}
var groupAllownaces=津贴.AsEnumerable().GroupBy(x=>x.Field(“empID”);
foreach(GroupAllowNames中的var组)
{
DataRow employeeRow=results.AsEnumerable()。其中(x=>x.Field(“empID”)==group.Key).First();
foreach(组中的数据行)
{
employeeRow[row.Field(“allow_name”)]=row.Field(“allow_Amount”);
}
}
var groupdeclusions=declusions.AsEnumerable().GroupBy(x=>x.Field(“empID”);
foreach(集团扣减中的var组)
{
DataRow employeeRow=results.AsEnumerable()。其中(x=>x.Field(“empID”)==group.Key).First();
foreach(组中的数据行)
{
employeeRow[行字段(“扣款名称”)]=行字段(“扣款金额”);
}
}
var groupAjenda=ajenda.AsEnumerable().GroupBy(x=>x.Field(“empID”);
foreach(groupAjenda中的var组)
{
DataRow employeeRow=results.AsEnumerable()。其中(x=>x.Field(“empID”)==group.Key).First();
foreach(组中的数据行)
{
employeeRow[行字段(“ajenda_名称”)]=行字段(“ajenda_金额”);
}
}
}
}
}

感谢您抽出时间分享您的问题。但是您的问题中缺少了一些东西。您的目标和困难是什么?到目前为止您做了什么?请尝试更好地解释您的问题、开发环境和数据结构,以及分享更多或更少的代码(无屏幕截图),一些示例、屏幕图像或草图,&用户故事或场景图。为了帮助您改进您的请求,请阅读&。如果您进行新发布,我有一个解决方案。没有理由关闭此发布。没有任何内容缺失。因此投票重新打开…@Ragy请根据表格给出预期结果的示例你提供的数据。还有,这是WinForms还是ASP.NET?还有,到目前为止你研究或尝试了什么?你到底被困在哪里了?请求是qu