I want to show Categories of Products I test two approaches : 1.
public ActionResult Index()
{
NORTHWNDEntities _db = new NORTHWNDEntities();
IList<ProductViewModel> pList = new List<ProductViewModel>();
foreach (var p in _db.ProductSet.Include("Category"))
{
ProductViewModel p1 = new ProductViewModel(){Name = p.ProductName,Price =p.UnitPrice ?? 0,Category = p.Category.CategoryName};
pList.Add(p1);
}
return View(pList);
}
2.
public ActionResult Index()
{
NORTHWNDEntities _db = new NORTHWNDEntities();
IList<ProductViewModel> pList = new List<ProductViewModel>();
foreach (var p in _db.ProductSet)
{
p.CategoryReference.Load();
ProductViewModel p1 = new ProductViewModel(){Name = p.ProductName,Price =p.UnitPrice ?? 0,Category = p.Category.CategoryName};
pList.Add(p1);
}
return View(pList);
}
I like second way because I hate magic strings.
I want to know is there another approaches for this ?
And which is better ?