Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{} in linq result

Tags:

c#

.net

linq

var query = from c in customers
                join o in orders on c.ID equals o.ID
                select new { c.ID, c.City, SalesBefore = c.Sales, NewOrder = o.Amount, SalesAfter = c.Sales + o.Amount };

    foreach (var item in query)
    {
        Console.WriteLine(item);
    }

I was running above code in console application. When it generates the result, it is displayed in the {}. Why is the result in the {}?

Example result

{ ID=1, City=New York.....}

There is nothing crucial. Just curious to know.

like image 462
gmail user Avatar asked Dec 03 '25 06:12

gmail user


2 Answers

That's just the default .ToString implementation of anonymous types. Nothing more or less.

like image 179
Kirk Woll Avatar answered Dec 04 '25 20:12

Kirk Woll


That code uses the Console.WriteLine(object) overload, which under the hood calls the .ToString() method on the object given. The .ToString() method, in turn, renders an anonymous type in that way.

like image 31
J. Steen Avatar answered Dec 04 '25 18:12

J. Steen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!