If the query does what you need and there aren't any performance problems then the only thing that can be improved is the formatting and naming.
You know that you can split the select into multiple lines? You also don't need the ()
becasue there is no extension attached to it like .ToList
etc. One name is inconsitent with the others: user_products
should be user_Has_Product
.
Will you know that name
is the user's name and not the product's name? The properties of the anonymous object should be PascalCase like all other property names.
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
};
This table doesn't look right Users_Has_Products
. Is this a look-up table? If so, I find the name UserProduct
prettier, without the underscore in the name and the has
that suggests it's something boolean. However when I see the properties user_Has_Product.quanitity
and user_Has_Product.created_at
I'm no longer sure about the look-up table theory.