0

This is in ASP.NET MVC 4,

I have a Portfolios Table and one of the properties of the Portfolio class is an ICollection of Stocks. Stocks have a string property: Symbol. If I have the portfolio object I am trying to check the symbol of a specific stock to see if it matches what I am looking for.

private PortfolioDb _db = new PortfolioDb();

public ActionResult Index([Bind(Prefix="id")]int portfolioId, 
    string searchTerm = null)
{
      var portfolio = _db.Portoflios
                      .Where(p=> searchTerm == null   || 
                               (p.Id == portfolioId && p.Stocks.Symbol == searchTerm)

So p.Stocks is my ICollection of Stocks and I want to check the symbol of all those stocks to see if they match the searchTerm. However, I cannot do p.Stocks.Symbol and I am wondering how I would access each symbol of each item in the Stocks collection.

Thanks

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
user2998266
  • 51
  • 1
  • 1
  • 7
  • you might need to combine queries using [groupby](http://stackoverflow.com/q/7325278/1698987) – Noctis Nov 16 '13 at 00:37
  • What do you want back? The stock objects? All portfolios with those stocks? – jfin3204 Nov 16 '13 at 00:48
  • I want to find the portfolio that has the matching portfolioId and has a Stock object in its list of Stocks that has a Symbol that matches the searchTerm. So 1 == 1 AND "MSFT" == "MSFT" – user2998266 Nov 16 '13 at 00:53

1 Answers1

2

Use .Any to find the portfolio that has any stock matching the searchterm::

&& p.Stocks.Any(s => s.Symbol == searchTerm)

jessehouwing
  • 106,458
  • 22
  • 256
  • 341