Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display grouped data by countries sorted by key

 using (NORTHWNDEntities baza = new NORTHWNDEntities())
    {
    var score = from customers in dataBase.Customers
                                group customers by customers .Country.ToUpper()
                                into sort
                                orderby sort.Key
                                select sort;
    foreach (var group in score )
                    {
                        Convert.ToString(group.Key);

                        foreach (var item in grupa)
                        {
                            var row = Convert.ToString(item.CompanyName) +               " : "
                             + Convert.ToString(item.Phone) + " : "
                             + Convert.ToString(item.Address);

                        }
                    }
     dataGridView1.DataSource = ???
     '''
      or
     '''
     listBox.DataSource = ???


}


What I have tried:

I tried this but this is score on one record in the group.

listScoreBox.DataSource = score.Select(s => new { s.Key, s.FirstOrDefault().CompanyName, s.FirstOrDefault().City }).ToList();
Posted
Updated 12-Apr-19 8:09am

1 solution

There is a lot that is wrong, or at least odd about your code.
Convert.ToString(group.Key);
Does absolutely nothing useful. Unless you store the result, or otherwise use it the converted value is discarded.
var row = Convert.ToString(item.CompanyName) +               " : "
 + Convert.ToString(item.Phone) + " : "
 + Convert.ToString(item.Address);
is inside a loop, so the value is discarded at the end of the loop body when row goes out of scope.

You need to sit down and decide what you are trying to do, and create either a useful collection or a datatable which holds the values you want to display, as separate properties, or columns.

Since I have no idea what your input data looks like, or what the heck is in grupa, or what you expect to show the user I can't tell you exactly what to do.
But a little thinking should get you there. Start by thinking about your inputs (unknown to us) and desired outputs (also unknown to us) and work out how you would get from one to the other manually - that should help you to work out what you need to tell the software to do.
 
Share this answer
 
Comments
Przemysław Szkaradek 12-Apr-19 14:28pm    
I want to display all results for each country in the group

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900