0

how to do it?

I've fount this code in How to Count Duplicates in List with LINQ :

 var list = new List<string> { "a", "b", "a", "c", "a", "b" };

        var q = from x in list
                group x by x into g
                let count = g.Count()
                orderby count descending
                select new { Value = g.Key, Count = count };
        foreach (var x in q)
        {
            MessageBox.Show("Value: " + x.Value + " Count: " + x.Count);
        }

But how to modify it to count duplicates in datagridview? For example datagridview1[7,i] where i is number of rows in datagriview.

EDIT

Now my code is looking like that:

      var list = dataGridView1.Rows.OfType<DataGridViewRow>()
       .GroupBy(x => x.Cells["TestValues"].Value)
       .Select(g => new { Value = g.Key, Count = g.Count(), Rows = g.ToList() })
       .OrderByDescending(x => x.Count);

        var q = from x in list
                group x by x into g
                let count = g.Count()
                orderby count descending
                select new { Value = g.Key, Count = count };

        foreach (var x in q)
        {
           // dataGridView1[7, x].Value.ToString();

            MessageBox.Show("Value: " + x.Value + " Count: " + x.Count +"Rows: " );

        }
Community
  • 1
  • 1
Elfoc
  • 3,649
  • 15
  • 46
  • 57

1 Answers1

2

Something like this should work:

var list = myDataGridView.Rows.OfType<DataGridViewRow>()
           .Select(x => x.Cells["MYCOLUMN"].Value.ToString());
var q = from x in list
    group x by x into g
    let count = g.Count()
    orderby count descending
    select new { Value = g.Key, Count = count };

where "MYCOLUMN" is the name of the column that you want, or, alternatively, you can pass the column index.

EDIT :

this code returns a list of items that contains also the list of rows with the duplications:

var q = myDataGridView.Rows.OfType<DataGridViewRow>()
        .GroupBy(x => x.Cells["MYCOLUMN"].Value.ToString())
        .Select(g => new {Value=g.Key, Count=g.Count(), Rows=g.ToList()})
        .OrderByDescending(x => x.Count);

so if you have 5 rows e.g. :

ID     MYCOLUMN
 0         A
 1         B
 2         C
 3         A
 4         B   

q will contain 3 elements:

 Key="A", Count=2, Rows={ [0 - A] [3 - A]}
 Key="B", Count=2, Rows={ [1 - B] [4 - B]}
 Key="C", Count=1, Rows={ [2 - C] }
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Tnx, this is working :) But is it possible to know in each rows duplication happened? – Elfoc Mar 07 '12 at 17:20
  • @Elfoc: check if my edit is what you need. (there could be some syntax errors because I didn't test it) – digEmAll Mar 07 '12 at 17:55
  • I'm getting message: Value: {Value= Test1, Count = 3, Rows = System.Collections.Generic.List'1[System.Windows.Forms.DataDgridViewRow]} Count:1 // Check Edit in my topic – Elfoc Mar 07 '12 at 18:11
  • 1
    @Elfoc: Sorry I forgot to mention that my last code already contains also the second query (it basically returns what you call "q"). You don't need to group the result of it. – digEmAll Mar 07 '12 at 19:40