Let’s say that I have tree tables User, Product and UserProduct.
A user can have many products, and a product can be assigned to many users.
I want to retrive all the users product with linq. I created this method in the User.cs class :
public List<Product> GetProductList()
{
List<Product> listProduct = new List<Product>();
List<UserProduct> listUserProduct = this.UserProdutcs.ToList();
foreach (var item in listUserProduct )
{
listProduct.Add(item.Product);
}
return listProduct;
}
I want to know if there is a better way of doing so ?
You could use
LINQand project into an anonymous type: