我正在使用asp.net核心实体框架。我正在使用linq来组合三个表,User,Products和User_Had_Products。我的查询似乎工作正常。我遇到的问题是打印出我的查询结果。我可以打印出整个对象,但不能使用点符号打印出各个字段。例如,对象的输出是
{ 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 }
我想要输出
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
这是我的代码
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);
}
答案 0 :(得分:1)
您必须格式化输出:
WriteLine($"{item.name} {item.product} {item.quantity} {item.date}");