开发者

Need help forming a LINQ to SQL expression

Got a table with column names as such

Name | ProductID | Price | Category | SubCategory

I am passing a category name into an action method and I want to return the subcategory names that are on the same row as the category name that is passed in.

So something like this, except this is wrong:

var subcategories = 
      productsRepository.Products
                        .Select(x => x.SubCategory开发者_开发百科.Where(x.Category == category);

So I can then collect the distinct subcategory names with this:

foreach (string subcategoryName in subcategories.Distinct().OrderBy(x => x))

Thanks.


Are you looking for:

var subcategories = productsRepository.Products
                                      .Where(x => x.Category == category)
                                      .Select(x => x.SubCategory)
                                      .Distinct()
                                      .OrderBy(x => x);

On another note, it does appear that your database isn't normalized.


I'm not sure I totally understand the problem, but wouldn't you be better off filtering initially and then selecting the column?

var subcategories = 
      productsRepository.Products.Where(x=>x.Category==category)
                        .Select(x => x.SubCategory);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜