C# SELECT DISTINCT的LINQ等价物

C# SELECT DISTINCT的LINQ等价物,c#,sql,linq,C#,Sql,Linq,我只想使用LINQ显示CourseNo的列表,没有重复项,但我不能完全理解语法 this.Distinct_CourseNo = (from c in Roster_Sections select c).Distinct(CourseNo).ToList; SQL等价物可以大致如下: SELECT DISTINCT CourseNo FROM Roster_Sections 使用select c将包括花名册部

我只想使用LINQ显示CourseNo的列表,没有重复项,但我不能完全理解语法

        this.Distinct_CourseNo = (from c in Roster_Sections
                                  select c).Distinct(CourseNo).ToList;
SQL等价物可以大致如下:

SELECT DISTINCT CourseNo
FROM Roster_Sections

使用
select c
将包括
花名册部分中的所有列
,因此如果列中至少有一行与另一行不同,则
Distinct()
将不起作用

this.Distinct_CourseNo = (from c in Roster_Sections
                          select c.CourseNo).Distinct().ToList();


使用
select c
将包括
花名册部分中的所有列
,因此如果列中至少有一行与另一行不同,则
Distinct()
将不起作用

this.Distinct_CourseNo = (from c in Roster_Sections
                          select c.CourseNo).Distinct().ToList();


您将传递
Distinct
一个lambda,描述什么定义了distincity:

this.Distinct_CourseNo = Roster_Sections.Distinct(x => x.CourseNo).ToList();

如果您想获得唯一课程编号列表,而不是基于非课程编号的唯一课程列表,今 草 顿 웃’s的答案是正确的。

您将通过
Distinct
一个lambda,描述什么定义了distincity:

this.Distinct_CourseNo = Roster_Sections.Distinct(x => x.CourseNo).ToList();

如果您想获得唯一课程编号列表,而不是基于非课程编号的唯一课程列表,今 草 顿 웃’s的答案是正确的。

提供的所有答案仅在您拥有的情况下适用。但是,您可以尝试以下方法:

this.Distinct_CourseNo = (from c in Roster_Sections
                          group c by c.CourseNo into g
                          select g.First()).ToList();

所有提供的答案仅适用于您拥有的情况。但是,您可以尝试以下方法:

this.Distinct_CourseNo = (from c in Roster_Sections
                          group c by c.CourseNo into g
                          select g.First()).ToList();

我使用提供的Linq方法得到以下错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”try,
this.Distinct_CourseNo=花名册_节。选择(c=>c.CourseNo.Distinct().ToList()我使用提供的Linq方法得到以下错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”try,
this.Distinct\u CourseNo=花名册\u节。选择(c=>c.CourseNo.Distinct().ToList()