Sie können nicht auf Product zurückgreifen, da dies Ihre Tabelle ist, die Sie abfragen. Sie brauchen eine anonyme Funktion, dann können Sie sie zu einem ViewModel hinzufügen, und jedes ViewModel zu einer List<MyViewModel>
und geben diese zurück. Es ist eine kleine Abschweifung, aber ich füge Vorbehalte über den Umgang mit nullable Daten, weil diese sind ein Schmerz in den Hintern zu behandeln, nur für den Fall, dass Sie einige haben. Dies ist, wie ich es behandelt.
Hoffentlich haben Sie eine ProductViewModel
:
public class ProductViewModel
{
[Key]
public string ID { get; set; }
public string Name { get; set; }
}
Ich habe Dependency Injection/Repository Framework, wo ich eine Funktion aufrufen, um meine Daten zu greifen. Mit Ihrem Beitrag als Beispiel, in Ihrem Controller-Funktion aufrufen, würde es wie folgt aussehen:
int categoryID = 1;
var prods = repository.GetProducts(categoryID);
In der Repository-Klasse:
public IEnumerable<ProductViewModel> GetProducts(int categoryID)
{
List<ProductViewModel> lstPVM = new List<ProductViewModel>();
var anonymousObjResult = from p in db.Products
where p.CategoryID == categoryID
select new
{
CatID = p.CategoryID,
Name = p.Name
};
// NOTE: If you have any dates that are nullable and null, you'll need to
// take care of that: ClosedDate = (DateTime?)p.ClosedDate ?? DateTime.Now
// If you want a particular date, you have to define a DateTime variable,
// assign your value to it, then replace DateTime.Now with that variable. You
// cannot call a DateTime.Parse there, unfortunately.
// Using
// new Date("1","1","1800");
// works, though. (I add a particular date so I can edit it out later.)
// I do this foreach below so I can return a List<ProductViewModel>.
// You could do: return anonymousObjResult.ToList(); here
// but it's not as clean and is an anonymous type instead of defined
// by a ViewModel where you can control the individual field types
foreach (var a in anonymousObjResult)
{
ProductViewModel pvm = new ProductViewModel();
pvm.ID = a.CatID;
pvm.Name = a.Name;
lstPVM.Add(rvm);
}
// Obviously you will just have ONE item there, but I built it
// like this so you could bring back the whole table, if you wanted
// to remove your Where clause, above.
return lstPVM;
}
Zurück in den Controller, Sie tun:
List<ProductViewModel> lstProd = new List<ProductViewModel>();
if (prods != null)
{
// For setting the dates back to nulls, I'm looking for this value:
// DateTime stdDate = DateTime.Parse("01/01/1800");
foreach (var a in prods)
{
ProductViewModel o_prod = new ReportViewModel();
o_prod.ID = a.ID;
o_prod.Name = a.Name;
// o_prod.ClosedDate = a.ClosedDate == stdDate ? null : a.ClosedDate;
lstProd.Add(o_prod);
}
}
return View(lstProd); // use this in your View as: @model IEnumerable<ProductViewModel>