简体   繁体   English

在linq中分组后选择对象类型列表

[英]Selecting list of object type after grouping in linq

I have a table such as the following: 我有如下表格:

ProductId, CategoryId 产品编号,类别编号
123, Category1 123,类别1
123, Category2 123,类别2
123, Category1 123,类别1

My parameter is productId and I need to return a list of type Category based on distinct categories for the given productId in the above table. 我的参数是productId,我需要根据上表中给定productId的不同类别返回类别类别的列表。

You could leverage the .Distinct() function from LINQ to select all the distinct categories belonging to the specified productId. 您可以利用LINQ的.Distinct()函数来选择属于指定productId的所有不同类别。

var pList = (from p in context.Products 
where p.ProductId == productId 
select p.Category).Distinct().ToList();
var list = context.Products
  .Where(p=>p.ProductId==productId)
  .Distinct();

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

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