如何在Datatable中比较两行? [英] How to Compare Two Rows in Datatable ?

查看:149
本文介绍了如何在Datatable中比较两行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个数据表



例如:

Id |名称|主题|标记

1 siva Phy 60

1 siva Che 80



我希望结果是单一的新数据表中的行



Id |名称|主题|标记|主题|标记

1 siva phy 60 che 80



如何在c#中执行此操作?

Hi,
I have a datatable

for eg :
Id | Name | Subject | Marks
1 siva Phy 60
1 siva Che 80

I want the result to be in a single row at new datatable

Id | Name | Subject | Marks | Subject | Marks
1 siva phy 60 che 80

how to do it in c# ?

推荐答案

你好Murugappan



DataTable 你不能有重复列。

你可以做的是

从表中获取不同的 ID和名称,为每个不同的行,您将 Subjects部分过滤到DataRow [] 数组,然后将数据数组加载到新表中。 as SUbject1,Marks1,Subject2,Marks2,Subject3,Marks3



我为你做过样品锻炼..

尝试deubg和根据你的需要修改..享受:)

Hi Murugappan

In DataTable you cannot have duplicate Columns.
What you can do is
Get distinct ID and Name from the table , for that each distinct row you filter the Subjects part into DataRow[] array and then load the array of data into the new table. as SUbject1,Marks1,Subject2,Marks2,Subject3,Marks3

I have done a sample workout for you..
Try to deubg and modify as per your need..enjoy :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Concurrent;
class Program
{
    public static void Main(string[] args)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Subject", typeof(string));
        dt.Columns.Add("Marks", typeof(int));
        dt.Rows.Add(1, "siva", "phy", 80);
        dt.Rows.Add(1, "siva", "che", 60);
        dt.Rows.Add(1, "siva", "mat", 50);
        dt.Rows.Add(1, "siva", "geo", 40);

        // consider max of 4 subjects
        DataTable dtout = new DataTable();
        dtout = dt.Clone();
        dtout.Columns.Add("Subject1", typeof(string)); dtout.Columns.Add("Marks1", typeof(int));
        dtout.Columns.Add("Subject2", typeof(string)); dtout.Columns.Add("Marks2", typeof(int));
        dtout.Columns.Add("Subject3", typeof(string)); dtout.Columns.Add("Marks3", typeof(int));

        DataView view = new DataView(dt);
       DataTable distinctValues = view.ToTable(true, "ID", "Name" );
       foreach (DataRow row in distinctValues.Rows)
       {
           string ID = row["ID"].ToString();
           string Name = row["Name"].ToString();
          var newrow =  dtout.NewRow();
           DataRow[] array = dt.Select("ID=" + ID + " AND Name='" + Name + "'");
           newrow[0] = array[0][0];
           newrow[1] = array[0][1];
           int j = 2;
           for (int i = 0; i < array.Length; i++)
           { 
               
                   newrow[j] = array[i][2];
                   newrow[j +1] = array[i][3];
                   j++; j++;
               
           }
           dtout.Rows.Add(newrow);
             
       }
       
        


    }
}


试试这个



try this

SELECT name, phy, che
FROM (
SELECT name, subject, marks
FROM marks) up
PIVOT (SUM(marks) FOR subject IN (phy, che)) AS pvt


这篇关于如何在Datatable中比较两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆