C# 如何在控制台应用程序中将具有值的行添加到现有datatable

C# 如何在控制台应用程序中将具有值的行添加到现有datatable,c#,console,C#,Console,我有一个数据表,我必须在它上面执行过滤,比如where,orderby。我有一张顾客姓名的清单。我想为每个customername筛选数据 我尝试了下面的代码 foreach (string customer in CustName) { Datarow[] DataDR = TradeFinanceBF3.Select(TradeFinanceBF3.Columns["Cust_Name"].ColumnName.Trim() + "='A'", "USD equi

我有一个数据表,我必须在它上面执行过滤,比如where,orderby。我有一张顾客姓名的清单。我想为每个customername筛选数据 我尝试了下面的代码

 foreach (string customer in CustName)
            {
 Datarow[] DataDR = TradeFinanceBF3.Select(TradeFinanceBF3.Columns["Cust_Name"].ColumnName.Trim() + "='A'", "USD equi DESC");
      }     
我得到datarow,然后如何将其传递给dataTable,以及如何将所有客户数据传递给同一个dataTable

我还尝试了LinkQuery来过滤数据,如下所示

      foreach (string customer in CustName)
            { 
 DataTable selectedTable = TradeFinanceBF3.AsEnumerable()
                          .Where(r => r.Field<string>("Cust_Name") == customer)
                          .OrderByDescending(r => r.Field<double>("IndexABC"))
                          .CopyToDataTable();

        ///Datable OutPut= ????? 

       }
foreach(CustName中的字符串customer)
{ 
DataTable selectedTable=TradeFinanceBF3.AsEnumerable()
其中(r=>r.Field(“客户名称”)==客户)
.OrderByDescending(r=>r.Field(“IndexABC”))
.CopyToDataTable();
///数据表输出=?????
}

我得到了datatable,但是如何将所有客户数据添加到一个datatable中?

您可以这样做:

DataRow[]result=TradeFinanceBF3.选择(“Cust_Name='A'”,“USD equi DESC”)

DataTable aux=TradeFinanceBF3.Clone()


你可以这样做:

DataRow[]result=TradeFinanceBF3.选择(“Cust_Name='A'”,“USD equi DESC”)

DataTable aux=TradeFinanceBF3.Clone()


我希望它能解决你的问题

    [Test]
    public void GetCustomerData()
    {
        DataTable TradeFinanceBF3 = GetTable();
        DataTable NewDatatable = TradeFinanceBF3.Clone();
        IList<string> CustName = new List<string> { "Janet", "David" };
        var selectedTable = (from dataRow in TradeFinanceBF3.AsEnumerable()
            join customerName in CustName on dataRow.Field<string>("Cust_Name") equals customerName
            select new
            {
                CustName = dataRow["Cust_Name"],
                IndexABC = dataRow["IndexABC"]
            }).OrderByDescending(p=>p.IndexABC);
        foreach (var table in selectedTable)
        {
            NewDatatable.Rows.Add(table.CustName, table.IndexABC);
        }
        Console.Write(NewDatatable);
    }

    private DataTable GetTable()
    {
        // Here we create a DataTable with four columns.
        DataTable table = new DataTable();
        table.Columns.Add("Cust_Name", typeof(string));
        table.Columns.Add("IndexABC", typeof(double));
        // Here we add five DataRows.
        table.Rows.Add("David", 1);
        table.Rows.Add("Sam", 2);
        table.Rows.Add("Christoff",2);
        table.Rows.Add("Janet", 4);
        table.Rows.Add("Melanie", 6);
        return table;
    }
[测试]
public void GetCustomerData()
{
DataTable TradeFinanceBF3=GetTable();
DataTable NewDatatable=TradeFinanceBF3.Clone();
IList CustName=新名单{“珍妮特”、“大卫”};
var selectedTable=(来自TradeFinanceBF3.AsEnumerable()中的数据行)
在dataRow上的CustName中加入customerName。字段(“Cust_Name”)等于customerName
选择新的
{
CustName=dataRow[“Cust_Name”],
IndexABC=dataRow[“IndexABC”]
}).OrderByDescending(p=>p.IndexABC);
foreach(selectedTable中的var表)
{
NewDatatable.Rows.Add(table.CustName,table.IndexABC);
}
Console.Write(NewDatatable);
}
私有数据表GetTable()
{
//这里我们创建一个包含四列的数据表。
DataTable=新的DataTable();
表.列.添加(“客户名称”,类型(字符串));
表.Columns.Add(“IndexABC”,typeof(double));
//这里我们添加五个数据行。
表.行.添加(“David”,1);
表.行.添加(“Sam”,2);
表.行.添加(“Christoff”,2);
表.行.添加(“Janet”,4);
表.行.加上(“Melanie”,6);
返回表;
}

我希望它能解决您的问题

    [Test]
    public void GetCustomerData()
    {
        DataTable TradeFinanceBF3 = GetTable();
        DataTable NewDatatable = TradeFinanceBF3.Clone();
        IList<string> CustName = new List<string> { "Janet", "David" };
        var selectedTable = (from dataRow in TradeFinanceBF3.AsEnumerable()
            join customerName in CustName on dataRow.Field<string>("Cust_Name") equals customerName
            select new
            {
                CustName = dataRow["Cust_Name"],
                IndexABC = dataRow["IndexABC"]
            }).OrderByDescending(p=>p.IndexABC);
        foreach (var table in selectedTable)
        {
            NewDatatable.Rows.Add(table.CustName, table.IndexABC);
        }
        Console.Write(NewDatatable);
    }

    private DataTable GetTable()
    {
        // Here we create a DataTable with four columns.
        DataTable table = new DataTable();
        table.Columns.Add("Cust_Name", typeof(string));
        table.Columns.Add("IndexABC", typeof(double));
        // Here we add five DataRows.
        table.Rows.Add("David", 1);
        table.Rows.Add("Sam", 2);
        table.Rows.Add("Christoff",2);
        table.Rows.Add("Janet", 4);
        table.Rows.Add("Melanie", 6);
        return table;
    }
[测试]
public void GetCustomerData()
{
DataTable TradeFinanceBF3=GetTable();
DataTable NewDatatable=TradeFinanceBF3.Clone();
IList CustName=新名单{“珍妮特”、“大卫”};
var selectedTable=(来自TradeFinanceBF3.AsEnumerable()中的数据行)
在dataRow上的CustName中加入customerName。字段(“Cust_Name”)等于customerName
选择新的
{
CustName=dataRow[“Cust_Name”],
IndexABC=dataRow[“IndexABC”]
}).OrderByDescending(p=>p.IndexABC);
foreach(selectedTable中的var表)
{
NewDatatable.Rows.Add(table.CustName,table.IndexABC);
}
Console.Write(NewDatatable);
}
私有数据表GetTable()
{
//这里我们创建一个包含四列的数据表。
DataTable=新的DataTable();
表.列.添加(“客户名称”,类型(字符串));
表.Columns.Add(“IndexABC”,typeof(double));
//这里我们添加五个数据行。
表.行.添加(“David”,1);
表.行.添加(“Sam”,2);
表.行.添加(“Christoff”,2);
表.行.添加(“Janet”,4);
表.行.加上(“Melanie”,6);
返回表;
}

Hye谢谢,它可以工作,但我想让我在第1栏下面的名字是另一个,我想让它在第1栏下面的名字是另一个,谢谢,它可以工作,但我想让我在第1栏下面的名字是另一个,我想让它在第1栏下面的名字是另一个,谢谢你的回复,但如果我有客户名单,那么我的代码会是什么?是否所有数据都存储在同一个数据表中以筛选和排序a
List
您可以使用
CustName.OrderBy(b=>b)。其中(a=>a.Contains('a')).ToList()谢谢…这对我来说非常有效。还有一个问题…如何更改datatable的背景色否,我想从datatable创建Excel工作表,然后如何更改datatable的背景色谢谢回答,但是如果我有客户名称列表,那么我的代码将如何?是否所有数据都存储在同一个数据表中以筛选和排序a
List
您可以使用
CustName.OrderBy(b=>b)。其中(a=>a.Contains('a')).ToList()谢谢…这对我来说非常有效。还有一个问题…如何更改datatable的背景色否,我想从datatable创建Excel工作表,然后如何更改datatable的背景色