Linq to Entities を使用した Lambda エクスセッションは初めてで、ここで助けを得たいと思っています。
ホームページで ViewModel を使用して、場所と会社の 2 つの列の下に記事のリストを表示しています。
article クラスを簡略化して表示すると、次のようになります。
public class Article
{
[Key]
public int ArticleID { get; set; }
public string Title { get; set; }
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
Location は次のようになります。
public class Location
{
[Key]
public int LocationID { get; set; }
public string LocationName { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
最後に、Company は次のようになります。
public class Company
{
[Key]
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
そのため、記事と企業、記事と場所の間には多対多の関係があります。私のページに表示したいのは、場所のリストに一致する記事と、会社のリストに一致する記事です。
私はビューモデルを持っています:
public class HomePageViewModel
{
public IEnumerable<Article> CompanyArticles { get; set; }
public IEnumerable<Article> LocationArticles { get; set; }
}
また、提供する会社と場所のリストに基づいて記事を返すラムダ式に苦労しています。すなわち:
public ActionResult Index()
{
var Companies = new List<Company>
{
new Company {CompanyName ="foo"},
new Company {CompanyName ="bar"}
};
var Locations= new List<Location>
{
new Location {LocationName ="UK"},
new Location {LocationName ="US"}
};
var viewModel = new HomePageViewModel();
viewModel.CompanyArticles = // what do I put here?
viewModel.LocationArticles = // what do I put here?
return View(viewModel);
}
よろしくお願いします。