0

I have a dataset with few rows and columns in it, in the columns state there will be many matching rows, i want all the rows with same state to bind it to some list..how to achieve it.

I need it because i have same capital in all the rows where i have same state, so i don' t want the loop to run each and every time...instead i want to bind all the rows where there is same capital and use that data.

  DataSet ds = new DataSet();
  da.Fill(ds, "table1");
  DataTable dt = new DataTable();
  dt = ds.Tables[0];

let me be more clear

i have a dataset with columns state, capital, population...i want to use this data in some other method so im using loop, when the state is tamilnadu, the capital and population will be same in all the rows..so its unnecessary to loop again and again for the data, so instead i want to bind the data with same state to something and use it..this is my requirement..

being fab
  • 657
  • 3
  • 12
  • 22

2 Answers2

2

Well, you can get the rows into a list by doing the following:

using System.Linq;

...

var rowsArray = new DataRow[dt.Rows.Count];
dt.Rows.CopyTo(rowsArray, 0);
var rowsList = rowsArray.ToList();

But as far as filtering based on the criteria you specified, that I'm unsure of because there's not near enough information here for that.

As an aside here, you could actually just filter the DefaultView by using something like:

dt.DefaultView.RowFilter = "some filter here";

And then just bind to the DefaultView. You can find documentation on that here.

Update for Edited Question

Okay, so based on what you want, here is a working console application that will do what you need. In short, you need to implement the EqualityComparer and add one line to the code above, the Distinct call with your comparer.

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

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<object[]>
            {
                new object[] {"tamilnadu","capital",2000000},
                new object[] {"other","capital",9490384},
                new object[] {"tamilnadu","capital",2000000}
            };

            foreach (var item in list)
            {
                Console.WriteLine(string.Format("{0} | {1} | {2}", item[0], item[1], item[2]));
            }

            Console.WriteLine();
            Console.WriteLine();

            var distinctList = list.Distinct(new MyArrayComparer());

            foreach (var item in distinctList)
            {
                Console.WriteLine(string.Format("{0} | {1} | {2}", item[0], item[1], item[2]));
            }
        }

        class MyArrayComparer : EqualityComparer<object[]>
        {
            public override bool Equals(object[] x, object[] y)
            {
                if (x.Length != y.Length) { return false; }
                for (int i = 0; i < x.Length; i++)
                {
                    if (!x[i].Equals(y[i]))
                    {
                        return false;
                    }
                }
                return true;
            }

            public override int GetHashCode(object[] obj)
            {
                return 0;
            }
        }
    }
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1
    List<YourObject> mylist = new List<YourObject>();
    YourObject obj;
    foreach (DataRow dr in dt.Rows)
    {
        obj = new YourObject()
        obj.PropertyA = dr["columnA"];
        obj.PropertyB = dr["columnB"];
        obj.PropertyB = dr["columnB"];
        mylist.Add(obj);
    }

P.S. you might witness some syntax errors since i am working on notepad, i don't have a compiler at the moment

You can as well use this way if you are using Linq: How do you convert a DataTable into a generic list?

Community
  • 1
  • 1
Alex
  • 5,971
  • 11
  • 42
  • 80
  • its good, but i want only the rows with same statename like, ex consider tamilnadu..only those rows i want to bind – being fab Oct 15 '12 at 11:45