0

如何将此查询从 sql 更改为 linq?

category
select distinct prodCategory from table4

subcategory
SELECT distinct [prodSubCat] FROM [table4] WHERE ([prodCategory] = @prodCategory)


prodCategory    prodSubCat
-------------------------------------
Home            Cassette Receiver
Home            Receiver
Home            cd player
Home            cd player
Home            Receiver
Car             dvd player
Car             GPS 

前任。

我的目标是为 prodCategory = "Car" 获取 prodSubCat

Car             dvd player
Car             GPS
4

2 回答 2

10

利用Distinct()

MSDN

从序列中返回不同的元素。

IEnumerable<string> category = (from p in t.table4
                                select p.prodCategory).Distinct();

IEnumerable<string> subCategory = (from p in t.table4
                                   where p.prodCategory == "Car"
                                   select p.prodSubCat).Distinct();
于 2013-10-29T13:11:49.423 回答
1

使用Distinct-> 通过使用默认的相等比较器来比较值,从序列中返回不同的元素。(MSDN:http: //msdn.microsoft.com/en-us/library/bb348436.aspx

var results = dataContext.Table4.Where(t => t.prodCategory.Equals(prodCategory)).Distinct();
于 2013-10-29T13:12:10.783 回答