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..

2 Answers 2

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;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

based on my requirement is there any other way better than converting datatable to list??
@beingfab, I'm not sure I really understand your requirement because it's not defined well enough. If you can put a lot more detail into the question I will be able to help you.
@beingfab, I've edited my answer to provide you what I think you want.
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?

1 Comment

its good, but i want only the rows with same statename like, ex consider tamilnadu..only those rows i want to bind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.