Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,

I retrived data from database. The data is

ID Speed
1 40
2 30
3 0
4 0
5 0
6 0
7 20

Here my problem is how to get the Speed = 0 records as one record in another dataset. see bleow output

ID Speed
1 40
2 30
3 to 6 0
7 20

Could anyone guide me how to get this output to another dataset.


Records
Nanda Kishore.CH
Posted
Comments
Member 9762654 19-Aug-13 2:55am    
are you using datatable?

 
Share this answer
 
You can use datatable.Select() method, you would have got the datarow collection then you can insert these row collection into your new dataset.

VB
Dim drFilter() As DataRow
drFilter = DataSet.Tables("TableName").Select("Speed = 0")
 
Share this answer
 
instead of using dataset use data table and try the below code
C#
 SqlDataAdapter da = new SqlDataAdapter("select id,speed from tablename", objCon);
int[] array = new int[5];
int i=0;
            DataTable DataTabledt1 = new DataTable("speed");
            da.Fill(DataTabledt1);
            DataColumn dc = DataTabledt1.Columns["speed"];
 DataColumn dc1 = DataTabledt1.Columns["id"];
            foreach (DataRow row in DataTabledt1.Rows)
            {
               
               if(Convert.ToInt32(row[dc].ToString())==0)
                  {
                      array[i]=Convert.ToInt32(row[dc1].ToString());
                  }
               
            }

if(array.count>0)
{
   Response.write(array[0]+"to"+array[array.count-1]+"0" )
}
 
Share this answer
 
C#
public class IDSpeed
    {
        public int Id { get; set; }
        public int Speed { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Create a demo list for ID and Speed
            List<IDSpeed> temp = new List<IDSpeed>();
            temp.Add(new IDSpeed { Id = 1, Speed = 15 });
            temp.Add(new IDSpeed { Id = 2, Speed = 23 });
            temp.Add(new IDSpeed { Id = 3, Speed = 0 });
            temp.Add(new IDSpeed { Id = 4, Speed = 0 });
            temp.Add(new IDSpeed { Id = 5, Speed = 0 });
            temp.Add(new IDSpeed { Id = 6, Speed = 0 });
            temp.Add(new IDSpeed { Id = 7, Speed = 72 });

            foreach (IDSpeed item in temp)
            {
                Console.WriteLine(item.Id + " " + item.Speed);
            }
            //Find all id which contains only zero
            var filteredRows = from n in temp.AsEnumerable()
                               where n.Speed.ToString().Contains("0")
                               select n;
            foreach (var item in filteredRows)
            {
                Console.WriteLine(item.Id);
            }
            Console.ReadKey();
        }
    }


JMD :-)
 
Share this answer
 
You can simply query the existing dataTable (the data which you have retrieved from the database must be in a datatable). Now, you can query that DataTable just like you query a database. I think you won't be able to use the DISTINCT constraint on DataTable but you can use utilize the WHERE clause to get your desired records.
Another way is to query the default dataview. There is a defaultView for every dataTable, you don't need to query that view but just use the filter property which is something like DataView.RowFilter. (Get more details for dataview at http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx[^]

Hope it will work for you :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900