1

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);
    }

よろしくお願いします。

4

2 に答える 2

0

あなたのViewModelは必要ないと本当に思います。多対多の関係の間に追加情報がないためです。

var articles = new List<Article>
{
    new Article {Title = "any1", Locations = new List<Location>(), 
                 Companies = new List<Company>()},
    new Article {Title = "any2", Locations = new List<Location>(), 
                 Companies = new List<Company>()}

};

articles[0].Companies.Add(Companies[0]);
articles[0].Locations.Add(Locations[0]);
于 2013-03-29T07:37:27.080 に答える