Robbie Mills
Robbie Mills

Reputation: 2945

Entity Framework Lambda expressions in a ViewModel and many to many relationships

I'm new to Lambda expessions with Linq to Entities and hope to get some help here.

I'm using a ViewModel on my home page to show a list of articles under 2 columns, Location and Company.

A simplified view of the article class looks like this:

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

}

And a Location looks like this:

public class Location
{
    [Key]
    public int LocationID { get; set; }

    public string LocationName { get; set; }

    public virtual ICollection<Article> Articles { get; set; }
}

And finally, a Company looks like this:

public class Company
{
    [Key]
    public int CompanyID { get; set; }

    public string CompanyName { get; set; }

    public virtual ICollection<Article> Articles { get; set; }
}

So I have a many to many relationship between articles and companies and articles and locations. What I'd like to display on my page are the articles that match a list of locations, and separately the articles that match a list of Companies.

I have a ViewModel:

public class HomePageViewModel
{
    public IEnumerable<Article> CompanyArticles { get; set; }
    public IEnumerable<Article> LocationArticles { get; set; }

}

And I'm struggling with the Lambda expressions to return the articles based on the list of companies and locations that I'll provide. Ie:

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

Thanks in advance for your help!

Upvotes: 1

Views: 766

Answers (2)

Evonet
Evonet

Reputation: 3640

This should be what you're after:

        viewModel.CompanyArticles = from a in db.Articles
                                    where
                                    (
                                    from c in a.Companies
                                    where InterestingCompanies.Contains(c.Name)
                                    select c
                                    ).Any()
                                    select a;


        viewModel.LocationArticles = from a in db.Articles
                                    where
                                    (
                                    from l in a.Locations
                                    where InterestingLocations.Contains(l.Name)
                                    select l
                                    ).Any()
                                    select a;

Upvotes: 1

lizonze292
lizonze292

Reputation: 1

I really think u don't need your ViewModel. Because you don't have additional information between the Many-to-Many relationship.

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

Upvotes: 0

Related Questions