LINQ不使用循环

时间:2016-04-06 22:55:03

标签: c# .net linq

在代码优化期间,我找到了在LINQ中使用foreach循环的方法。我想在没有这个循环的情况下使用它。有什么建议我怎么改变它?

select *
from ADDRESS a
join ADDRESS_history using (ID)
MEMBER_STATUS = 2 

我尝试了这个但是它给了我错误信息"标识符预期":

ID | FIRSTNAME | LASTNAME | MEMBER_STATUS | DTM
1  | Tom       | Mueller  | 1             | 2015-03-30
2  | John      | Doe      | 2             | 2016-03-02
3  | David     | Allen    | 2             | 2013-03-01

1 个答案:

答案 0 :(得分:1)

可读和最短的方式:

public IEnumerable<Tuple<string, string>> ListAllCoursesWithArea()
{
    return bookListLoader
                .LoadList()
                .GroupBy(x => x.CourseCode)
                .Select(g => g.First())
                .Select(x => new Tuple<string, string>(x.CourseCode, x.Area));
}

或者在你的例子中:

public IEnumerable<Tuple<string, string>> ListAllCoursesWithArea()
{
    return from temp in bookListLoader.LoadList()
           group temp by new { temp.CourseCode } into g
           let x = g.First()
           select new Tuple<string, string>(x.CourseCode, x.Area);

}