2012-11-15 245 views
2

在MySQL中,转换查询Lambda表达式

select distinct(trim(containerType)) from productIn where containerType <> '' group by containerNo 

我怎么能作出这样的查询使用Lambda表达式?

例)

List<string> containerTypes = new List<string>(); 
containerTypes = productInRepository.GroupBy(x=> x.containerNo).Select(?????).ToList(); 

enter image description here

+0

我已经完成了MySql,但你的SQL甚至是合法的*已经有一段时间了?在不包括在组中的字段上进行选择? –

+0

我只是测试它,是的,它的工作原理 –

回答

1

我认为GROUPBY字段,它是不是在结果选择装置相同的OrderBy此字段。

List<string> containerTypes = productInRepository 
       .Where(x => x.containerType != string.Empty) 
       .OrderBy(x => x.containerNo) 
       .Select(x => x.containerType.Trim()) 
       .Distinct(); 
1
List<string> containerTypes = productInRepository 
    .Where(x => x.containerType != string.Empty) 
    .GroupBy(x=> x.containerNo) 
    .Select(x => x.containerType.Trim()) 
    .ToList();