C# Querying With LINQ Query Operators Aggregates

Radu - Adrian Buha
PRO
Radu - Adrian Buha
Pro Student 4,504 Points

How can I display the result of the queries from this video inside a console app in Visual Studio?

Hello,

Because the REPL is a bit unreliable for this section of C# and because I wanted to learn better these queries, I want to implement these queries in Console Application in Visual Studio.

Therefore, my question is, how can I display these queries results in the Console Window?

Thank you!

6 Answers

Steven Parker
Steven Parker
176,294 Points

:mailbox_with_mail: I got your request.

I'm used to using Entity Framework where you deal with the database in a way where results come back in an object which has properties that represent the various columns returned by the query.. There are several courses here covering the use of EF.

But you can also make queries using a more fundamental mechanism involving a Command Object. Then you can retrieve the results with an Execution Reader or Table Adapter. See this MSDN page on How to Create and Execute an SQL Statement that Returns Rows.

Radu - Adrian Buha
PRO
Radu - Adrian Buha
Pro Student 4,504 Points

All I want is to print to Console Window this query:

var groupedBirds = birds.GroupBy(b => b.Color);

But if I try to print it with the foreach loop I get this result:

System.Linq.Lookup'2+Grouping[System.String,Aggregates.Bird] System.Linq.Lookup'2+Grouping[System.String,Aggregates.Bird] System.Linq.Lookup'2+Grouping[System.String,Aggregates.Bird] System.Linq.Lookup'2+Grouping[System.String,Aggregates.Bird] System.Linq.Lookup'2+Grouping[System.String,Aggregates.Bird]

I want to get the same result (or at least similar) in the Console Window as Carling gets in the Aggregates tutorial in REPL.

Any ideeas?

Thanks in advance.

Radu - Adrian Buha
PRO
Radu - Adrian Buha
Pro Student 4,504 Points

Found my solution. I just needed two foreach, one inside the other:

foreach (var g in groupedBirds) { Console.WriteLine(g.Key); foreach (var ge in g) { Console.WriteLine(ge); }

        }
Steven Parker
Steven Parker
176,294 Points

This doesn't seem related to the original question, am I missing something?

Radu - Adrian Buha
PRO
Radu - Adrian Buha
Pro Student 4,504 Points

My question was related on the Aggregates tutorial from Linq Queries. I wanted to display the results for those specific queries from the Aggregates tutorial. I apologize if my original question seemed ambiguous, but English is not my first language. Many thanks for your replies.