Answered Hi

  • Saturday, June 09, 2012 3:42 PM
     
     

    datatable.select(filter expression, sort). here the filter expression which accepts the following condition?

    Condition => datatable.select("UNIQUE NAME","NAME");

    Here NAME Indicates the column in the table.....

    Name

    AAAA

    BBBB

    CCCC

    AAAA

    DDDD

    CCCC

    EEEE

    Is it possible to get the unique name using filter condition?

    Thanks & Regards

    Manikandan Murugeshan


    Manikandan Murugeshan

All Replies

  • Saturday, June 09, 2012 4:43 PM
     
      Has Code

    What do you mean by Unique name? Not duplicates?

    If so, take a look into this example I did:

            static void Main(string[] args)
            {
                DataTable table = new DataTable();
                table.Columns.Add("name", typeof(string));
                table.Rows.Add("aaa");
                table.Rows.Add("bbb");
                table.Rows.Add("ccc");
                table.Rows.Add("aaa");
                table.Rows.Add("bbb");
    
                DataTable newTable = RemoveDuplicateRows(table, "name");
            }
    
            public static DataTable RemoveDuplicateRows(DataTable dTable, string colName)
            {
                Hashtable hTable = new Hashtable();
                ArrayList duplicateList = new ArrayList();
    
                //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
                //And add duplicate item value in arraylist.
                foreach (DataRow drow in dTable.Rows)
                {
                    if (hTable.Contains(drow[colName]))
                        duplicateList.Add(drow);
                    else
                        hTable.Add(drow[colName], string.Empty);
                }
    
                //Removing a list of duplicate items from datatable.
                foreach (DataRow dRow in duplicateList)
                    dTable.Rows.Remove(dRow);
    
                //Datatable which contains unique records will be return as output.
                return dTable;
            }


    Mitja

  • Saturday, June 09, 2012 11:23 PM
    Moderator
     
     Answered
    You could use the .ToTable(true) method of a DataView (or use the DataTable.DefaultView if you prefer). You can sort and filter a DataView. The DataView.ToTable(true) indicates that you wish distinct values. Try playing with it and see if it'll accomplish what you need ...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • Sunday, June 10, 2012 1:28 AM
     
      Has Code

    >>Is it possible to get the unique name using filter condition?

    You can also do this:

    public class MainTest
        {
            private class MyComparer : IEqualityComparer<DataRow>
            {
                public bool Equals(DataRow x, DataRow y)
                {
                    if (x[0] == null && y[0] == null)
                    {
                        return true;
                    }
                    else
                    {
                        if (x[0] == null || y[0] == null)
                        {
                            return false;
                        }
                        else
                        {
                            return x[0].ToString() == y[0].ToString();
                        }
                    }
                }
    
                public int GetHashCode(DataRow obj)
                {
                    return obj[0].GetHashCode();
                }
            }
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("name");
                dt.Rows.Add("AAA");
                dt.Rows.Add("BBB");
                dt.Rows.Add("AAA");
                dt.Rows.Add("CCC");
                dt.Rows.Add("AAA");
                dt.Rows.Add("CCC");
    
                var result = ((from row in dt.Rows.Cast<DataRow>()
                              select row).Distinct(new MyComparer())).ToArray();
    
                foreach (var item in result)
                {
                    Console.WriteLine(item[0].ToString());
                }             
            }
        }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

  • Sunday, June 10, 2012 12:27 PM
     
      Has Code

    Hi Mitja,

    Am sorry . am not clearly defined my problem. My problem is i need to filter the Unique name in the "foreach" statement or using the keyword "UNIQUE" in "foreach" statement.

    DataTable table = new DataTable(); table.Columns.Add("name", typeof(string));

    table.Columns.Add("age",typeof(string));

    E.g => foreach(DataRow dt in datatable.select("Name='AAAA' and age > 24" )

    {

    }

    In any DB we using query just as like this "SELECT * FROM datatable where NAME='AAAA' and age > 24". Same i used in "foreach" statement.

    Same as we can use "SELECT DISTINCT NAME FROM datatable where age >24"

    Like wise , is it possible to use "UNIQUE" keyword in "foreach" statement.

    E.g : foreach(DataRow dt in datatable.select("UNIQUE NAME"))// it's not working, it's getting failed. any other alternate way?

    {

    }


    Manikandan Murugeshan


  • Sunday, June 10, 2012 12:30 PM
     
      Has Code

    Hi,

    Am sorry . am not clearly defined my problem. My problem is i need to filter the Unique name in the "foreach" statement or using the keyword "UNIQUE" in "foreach" statement.

    DataTable table = new DataTable(); table.Columns.Add("name", typeof(string));

    table.Columns.Add("age",typeof(string));

    E.g => foreach(DataRow dt in datatable.select("Name='AAAA' and age > 24" )

    {

    }

    In any DB we using query just as like this "SELECT * FROM datatable where NAME='AAAA' and age > 24". Same i used in "foreach" statement.

    Same as we can use "SELECT DISTINCT NAME FROM datatable where age >24"

    Like wise , is it possible to use "UNIQUE" keyword in "foreach" statement.

    E.g : foreach(DataRow dt in datatable.select("UNIQUE NAME"))// it's not working, it's getting failed. any other alternate way?

    {

    }


    Manikandan Murugeshan

  • Sunday, June 10, 2012 12:31 PM
     
      Has Code

    Hi ,

    Am sorry . am not clearly defined my problem. My problem is i need to filter the Unique name in the "foreach" statement or using the keyword "UNIQUE" in "foreach" statement.

    DataTable table = new DataTable(); table.Columns.Add("name", typeof(string));

    table.Columns.Add("age",typeof(string));

    E.g => foreach(DataRow dt in datatable.select("Name='AAAA' and age > 24" )

    {

    }

    In any DB we using query just as like this "SELECT * FROM datatable where NAME='AAAA' and age > 24". Same i used in "foreach" statement.

    Same as we can use "SELECT DISTINCT NAME FROM datatable where age >24"

    Like wise , is it possible to use "UNIQUE" keyword in "foreach" statement.

    E.g : foreach(DataRow dt in datatable.select("UNIQUE NAME"))// it's not working, it's getting failed. any other alternate way?

    {

    }


    Manikandan Murugeshan

  • Sunday, June 10, 2012 12:33 PM
     
     Answered
    DataView.ToTable allows selecting distinct values.  See the example here: http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx.  Hopefully this is what you are trying to achieve.

    --
    Mike
  • Sunday, June 10, 2012 12:40 PM
     
      Has Code

    Hi Mike,

    Am sorry . am not clearly defined my problem. My problem is i need to filter the Unique name in the "foreach" statement or using the keyword "UNIQUE" in "foreach" statement.

    DataTable table = new DataTable(); table.Columns.Add("name", typeof(string));

    table.Columns.Add("age",typeof(string));

    E.g => foreach(DataRow dt in datatable.select("Name='AAAA' and age > 24" )

    {

    }

    In any DB we using query just as like this "SELECT * FROM datatable where NAME='AAAA' and age > 24". Same i used in "foreach" statement.

    Same as we can use "SELECT DISTINCT NAME FROM datatable where age >24"

    Like wise , is it possible to use "UNIQUE" keyword in "foreach" statement.

    E.g : foreach(DataRow dt in datatable.select("UNIQUE NAME"))// it's not working, it's getting failed. any other alternate way?

    {

    }


    Manikandan Murugeshan

  • Sunday, June 10, 2012 12:44 PM
     
     
    I suggested an alternative way.  I hope you find what you are seeking.

    --
    Mike
  • Sunday, June 10, 2012 2:39 PM
     
     

    Hi Mike,

    First i apologize..... I didnt fully seen that Page.....  I looked for what i am seeking only.. then only i gone through the page fully, the example is very much similar to what i am looking for... really thanks a lot.....


    Manikandan Murugeshan

  • Sunday, June 10, 2012 4:15 PM
    Moderator
     
     
    That was my suggestion too (using DataView.ToTable(true)) ... but I didn't provide any links to look at, so I guess you didn't pay any attention. Oh well ... I like talking to myself. LOL!

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • Sunday, June 10, 2012 9:15 PM
     
     
    Sorry, somehow I missed what you wrote too.  I will try reading your posts more often!

    --
    Mike
  • Sunday, June 10, 2012 9:47 PM
    Moderator
     
     
    Hey,  no problem Mike! I was actually replying to Manikandan, not you ... and it was simply my attempt at being silly.  ;0)

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • Monday, June 11, 2012 2:53 AM
     
     Answered Has Code

    Manikandan Murugeshan:-)

    One problem can probably have many ways to solve itself……Have you tried mine?!

    Haha……It's time for me you offer you a WHOLE solution:)

    In fact you can use DataView to filter with some conditions,and then use ToTable to convert to the DataTable and then use my extended method:-)

    public class MyComparer : IEqualityComparer<DataRow>
    {
        public DataColumn Column { get; set; }
    
        public MyComparer(DataColumn _col)
        {
            Column = _col;
        }
    
        public bool Equals(DataRow x, DataRow y)
        {
            if (x[Column] == null && y[Column] == null)
            {
                return true;
            }
            else
            {
                if (x[Column] == null || y[Column] == null)
                {
                    return false;
                }
                else
                {
                    return x[Column].ToString() == y[Column].ToString();
                }
            }
        }
    
        public int GetHashCode(DataRow obj)
        {
            return obj[Column].GetHashCode();
        }
    }
    
    public static class DataTableUniqueExtender
    {
        public static IEnumerable<string> GetUniqueFieldValues(this DataTable dt,Func<DataRow,bool>condition, string columnName)
        {
            //Tell whether the column exists in the DataTable
            if (dt.Columns.IndexOf(columnName) < 0)
            {
                throw new Exception("Column doesn't exist!");
            }
    
            var result = dt.AsEnumerable().Where(condition).Select(s => s).Distinct(new MyComparer(dt.Columns[columnName])).Select(s=>s[columnName].ToString());
            return result;
        }
    }
    
    public class MainTest
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("id");
            dt.Rows.Add("AAA", 1);
            dt.Rows.Add("AA", 2);
            dt.Rows.Add("AAA", 3);
            dt.Rows.Add("AAA", 1);
            dt.Rows.Add("AAA", 4);
            dt.Rows.Add("CCC", 2);
    
            var result = dt.GetUniqueFieldValues((row) => { return row["name"].ToString() == "AAA"; }, "id");
    
            foreach (var item in result)
            {
                Console.WriteLine(item[0].ToString());
            }
        }
    }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处