Ali Ahmadi
Ali Ahmadi

Reputation: 2417

How can I bind group by query to DataGridview in linq?

I want bind the below query to DataGridview. how can I it?

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numberGroups =
                 from num in numbers
                 group num by num % 5 into numGroup
                 select new { Remainder = numGroup.Key, Numbers = numGroup };

Upvotes: 0

Views: 1431

Answers (1)

jrob
jrob

Reputation: 532

How do you want it represented in the DGV since Numbers could contain more than one item how would this go into a DGV row?

Alternatively this should work:

var numberGroups = from num in numbers
                   group num by num % 5 into numGroup
                   select new { Remainder = numGroup.Key, Count = numGroup.Count() };

yourGridView.DataSource = numberGroups.ToList();
yourGridView.DataBind();  //don't use if WinForm DGV

Upvotes: 1

Related Questions