Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Querying With LINQ Query Operators Aggregates

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 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
231,268 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
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 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
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 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
231,268 Points

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

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 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.