My Product class is
public class Product
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public string ProductName { get; set; }
public virtual ICollection<ProductColor> ProductColors { get; set; }
}
The Color class
public class Color
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ColorID { get; set; }
public string ColorName { get; set; }
public virtual ICollection<ProductColor> ProductColors { get; set; }
}
and the intermediate class for creating many to many relationship
public class ProductColor
{
public int ProductID { get; set; }
public int ColorID { get; set; }
public virtual Product Product { get; set; }
public virtual Color Color { get; set; }
}
Supposing my product model contains
ProductID ProductName
1 Product 1
2 Product 2
3 Product 3
My color model contains
ColorID ColorName
1 Red
2 Green
3 Blue
And the intermediate model contains
ProductID ColorID
1 1
1 2
2 3
How can i write a Linq query to get all the colors for each Product in a list?