简体   繁体   English

需要帮助来形成LINQ to SQL表达式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM