1

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!

2 Answers 2

1

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;
1
  • Perfect! Exactly what I was after. Apr 5, 2013 at 6:50
0

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]);
1
  • Thanks for that, but I thought I needed a Viewmodel as I'm displaying multiple models in my view? Also the issue that I'm trying to solve is the linq query to get all the articles by a list of locations and companies - I can't see how your answer will show me that... Mar 29, 2013 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.