2013-10-29 82 views
0

该查询如何从sql更改为linq?在Linq中使用Distinct到SQL

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 

Ex。

我的目标是让prodSubCat为prodCategory = “驾驶”

Car    dvd player 
Car    GPS 

回答

8

使用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(); 
+0

table4.AsEnumerable()。 – terrybozzio

+0

@terrybozzio table4是一个实体。 –