c# - Count distinct values of a column in dataGridView using linq in .NET -


i need count , present distinct/unique values in datagridview. want present this, , code works fine lists.

        list<string> aryids = new list<string>();         aryids.add("1234");         aryids.add("4321");         aryids.add("3214");         aryids.add("1234");         aryids.add("4321");         aryids.add("1234");          var result= aryids.groupby(id => id).orderbydescending(id => id.count()).select(g => new { id = g.key, count = g.count() }); 

but when try use same approach on column in datagridview error saying groupby cannot used on datagridview.

        datagridview datagridview1= new datagridview();         datagridview1.columns.add("nr", "nr");         string[] row1 = new string[] { "1234" };         string[] row2 = new string[] { "4321" };         string[] row3 = new string[] { "3214" };         string[] row4 = new string[] { "1234" };         string[] row5 = new string[] { "4321" };         string[] row6 = new string[] { "1234" };          object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };          foreach (string[] rowarray in rows)         {             datagridview1.rows.add(rowarray);         }          var result = datagridview1.groupby(id => id).orderbydescending(id => id.count()).select(g => new { id = g.key, count = g.count() }); 

so question is, how adapt linq syntax work column in datagridview? if possible, dont want use lists @ all.

this work example:

var result = datagridview1.rows.cast<datagridviewrow>()     .where(r => r.cells[0].value != null)     .select (r => r.cells[0].value)     .groupby(id => id)         .orderbydescending(id => id.count())          .select(g => new { id = g.key, count = g.count() }); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -