Aaron
Aaron

Reputation: 4480

Printing out the results of my linq query

I am using asp.net core entity framework. I am using linq to combine three tables, User, Products, and User_Had_Products. My query seems to be working fine. The problem I am having is printing out the results of my query. I can print out the entire object, but cannot use dot notation to print out the individual fields. For example the output for the object is

{ name = user, product = desk, quantity = 1, date = 1/13/17 3:30:32 PM }
{ name = user, product = desk, quantity = 1, date = 1/13/17 3:52:40 PM }
{ name = user, product = desk, quantity = 1, date = 1/13/17 3:52:40 PM }
{ name = user, product = desk, quantity = 1, date = 1/14/17 9:14:22 AM }
{ name = user, product = desk, quantity = 1, date = 1/14/17 9:14:27 AM }

What I want for output in

user  desk 1 1/13/17 3:30:32 PM 
user  desk 1 1/13/17 3:30:32 PM 
user  desk 1 1/13/17 3:30:32 PM 
user  desk 1 1/13/17 3:30:32 PM 
user  desk 1 1/13/17 3:30:32 PM 

Here is my code

 ViewBag.User_Has_Products = from user_products in _context.Users_Has_Products
                    join user in _context.Users on user_products.users_id equals user.id 
                    join product in _context.Products on user_products.products_id equals product.id 
                    select new{name =  user.name, product = product.name, 
                    quantity = user_products.quanitity, date = user_products.created_at};

            foreach(var item in ViewBag.User_Has_Products)
            {
                System.Console.WriteLine(item);
            }

Upvotes: 1

Views: 996

Answers (1)

H.G. Sandhagen
H.G. Sandhagen

Reputation: 822

You have to format your output:

WriteLine($"{item.name} {item.product} {item.quantity} {item.date}");

Upvotes: 1

Related Questions