过滤数据集中的数据并返回到另一个数据集 [英] Filter the Data in dataset and return to another dataset

查看:98
本文介绍了过滤数据集中的数据并返回到另一个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在dataset1中有一个User Details,而Dataset1在表,Name,Reg_Date,CityID,LocationID,LocationName,Reg_For中有以下字段。在后面的代码我需要取一个userdetails取决于他们在dataset1中的位置和过滤后的数据存储到dataset2 ..任何人给出建议plz?

解决方案

For Filter Data in数据集使用选择过滤数据的方法,它返回数据行。

现在可以将这些数据行添加到新数据集中。


I已经为你写了一个演示代码,只是在这里得到了想法,并检查你提到的实际数据类型,如我所提到的字符串,日期时间,整数...

只需检查你的方式

但它应该是这样的



  var  filterdata =(< span class =code-sdkkeyword> from  f  in  dataset1.Tables [ 0 ] .AsEnumerable()
其中 f.Field< string>( 禄ation)== India
选择 new
{
Name = f.Field< string>( 名称),
Reg_Date = f.Field< DateTime>( Reg_Date),
CityID = f.Field< int>( < span class =code-string> CityID),
LocationID = f.Field< int>( LocationID),
LocationName = f.Field< string>( LocationName),
Reg_For = f.Field< string>( Reg_For
})。ToList( );
foreach var item in filterdata)
{
DataRow row = new DataRow();
row [ 名称] = item.Name;
row [ Reg_Date] = item.Reg_Date;
row [ CityID] = item.CityID;
row [ LocationID] = item.LocationID;
row [ LocationName] = item.LocationName;
row [ Reg_For] = item.Reg_For;
dataset2.Tables [ 0 ]。Rows.Add(row);
}


如果您将使用它,这将很容易。



< pre lang =c#> DataTable table1 = new DataTable( 注册);
table1.Columns.Add( Location);
table1.Columns.Add( Id);
table1.Rows.Add( Loc1 1 );
table1.Rows.Add( Loc2 2 );
table1.Rows.Add( Loc3 3 );
table1.Rows.Add( Loc1 4 );
table1.Rows.Add( Loc4 5 );
table1.Rows.Add( Loc1 6 );
table1.Rows.Add( Loc1 7 );
table1.Rows.Add( Loc5 8 );


DataTable table2 = new DataTable( Registration1\" );
table2.Columns.Add( Location);
table2.Columns.Add( id);

DataSet Dset = new DataSet( DSET1);
DataSet Dset1 = new DataSet( DSET2 );
Dset.Tables.Add(table1);

if (Dset.Tables [ 0 ]。Rows.Count > 0
{
for int i = 0 ; i < ; Dset.Tables [ 0 ]。Rows.Count; i ++)
{
DataRow dr = Dset.Tables [ 0 ]行[i]中。;
if (dr [ Location]。ToString()== Loc1
{
table2.Rows.Add(dr [ Location]。ToString(),dr [ Id]。ToString());


}
}
Dset1.Tables.Add(table2);
}







好​​运。


I have a User Details in dataset1, and Dataset1 have the following fields in table, Name,Reg_Date,CityID,LocationID,LocationName,Reg_For. In code behind i need to take a userdetails depend on their location from dataset1 and filtered data's stored into dataset2.. Anyone give a suggestions plz?

解决方案

For Filter Data in Data Set Use Select Method for Filtering data, it Returns Datarows.
You can now add these datarows into new dataset.


I have written a demo code for you just get the idea here, ANd check your actual data type where i have mentioned like string, datetime,int...
Just check your way
But it should like this

var filterdata = (from f in dataset1.Tables[0].AsEnumerable()
                          where f.Field<string>("Location") == "India"
                          select new
                          {
                              Name = f.Field<string>("Name"),
                              Reg_Date = f.Field<DateTime>("Reg_Date"),
                              CityID = f.Field<int>("CityID"),
                              LocationID = f.Field<int>("LocationID"),
                              LocationName = f.Field<string>("LocationName"),
                              Reg_For = f.Field<string>("Reg_For")
                          }).ToList();
        foreach (var item in filterdata)
        {
            DataRow row = new DataRow();
            row["Name"] = item.Name;
            row["Reg_Date"] = item.Reg_Date;
            row["CityID"] = item.CityID;
            row["LocationID"] = item.LocationID;
            row["LocationName"] = item.LocationName;
            row["Reg_For"] = item.Reg_For;
            dataset2.Tables[0].Rows.Add(row);
        }


This would be easy if you will use this.

DataTable table1 = new DataTable("Registration");
      table1.Columns.Add("Location");
      table1.Columns.Add("Id");
      table1.Rows.Add("Loc1", 1);
      table1.Rows.Add("Loc2", 2);
      table1.Rows.Add("Loc3", 3);
      table1.Rows.Add("Loc1", 4);
      table1.Rows.Add("Loc4", 5);
      table1.Rows.Add("Loc1", 6);
      table1.Rows.Add("Loc1", 7);
      table1.Rows.Add("Loc5", 8);


      DataTable table2 = new DataTable("Registration1");
      table2.Columns.Add("Location");
      table2.Columns.Add("id");

      DataSet Dset = new DataSet("DSET1");
      DataSet Dset1 = new DataSet("DSET2");
      Dset.Tables.Add(table1);

      if (Dset.Tables[0].Rows.Count > 0)
      {
          for (int i = 0; i < Dset.Tables[0].Rows.Count; i++)
          {
              DataRow dr = Dset.Tables[0].Rows[i];
              if (dr["Location"].ToString() == "Loc1")
              {
                  table2.Rows.Add(dr["Location"].ToString(),dr["Id"].ToString());


              }
          }
          Dset1.Tables.Add(table2);
      }




Best of luck.


这篇关于过滤数据集中的数据并返回到另一个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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