Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按多列返回linq组的类型_C#_Linq - Fatal编程技术网

C# 按多列返回linq组的类型

C# 按多列返回linq组的类型,c#,linq,C#,Linq,我有以下代码: var studentClassAttendancesGrouped = classAttendanceByCriteria.OrderBy(x => x.Lecture.Id).GroupBy(x => new { x.Lecture, x.Teacher }); 我试图从一个方法返回studentClassAttendancesGrouped。但我无法确定该方法的返回类型。GroupBy的重载将返回IEn

我有以下代码:

 var studentClassAttendancesGrouped =
                            classAttendanceByCriteria.OrderBy(x => x.Lecture.Id).GroupBy(x => new { x.Lecture, x.Teacher });
我试图从一个方法返回
studentClassAttendancesGrouped
。但我无法确定该方法的返回类型。

GroupBy的
重载将返回
IEnumerable
,如图所示

您只需要使用投影您的可枚举项,当然,您必须在末尾调用以执行查询并创建列表:

var res = classAttendanceByCriteria.OrderBy(x => x.Lecture.Id)
                                   .GroupBy(x => new { x.Lecture, x.Teacher })
                                   .Select(x => new {x.Key.Lecture, x.Key.Teacher})
                                   .ToList();
但是,上面的代码创建了一个匿名类型列表。如果要从方法返回上述语句,则需要使用或创建自定义对象并返回以下列表:

public class LectureTeacher
{
    public string Lecture { get; set; }
    public string Teacher { get; set; }
}

public List<LectureTeacher> GetDogsWithBreedNames()
{
    var res = classAttendanceByCriteria.OrderBy(x => x.Lecture.Id)
                                       .GroupBy(x => new { x.Lecture, x.Teacher })
                                       .Select(x => new LectureTeacher {Lecture  = x.Key.Lecture, Teacher  = x.Key.Teacher})
                                       .ToList();

    return res;
}

我建议您创建一个自定义类,并从方法返回它:-

IEnumerable<CustomType> studentClassAttendancesGrouped =
       classAttendanceByCriteria.OrderBy(x => x.Lecture.Id)
       .GroupBy(x => new { x.Lecture, x.Teacher })
      .Select(x => new CustomType { Lecture = x.Key.Lecture, Teacher = x.Key.Teacher });
您可以根据需要添加更多列,最后从方法返回
IEnumerable

因此,您的方法如下所示:-

public IEnumerable<CustomType> MyMethod()
{
    return classAttendanceByCriteria.OrderBy(x => x.Lecture.Id)
       .GroupBy(x => new { x.Lecture, x.Teacher })
       .Select(x => new CustomType { Lecture = x.Key.Lecture, Teacher = x.Key.Teacher });
}
public IEnumerable MyMethod()
{
return classAttendanceByCriteria.OrderBy(x=>x.touch.Id)
.GroupBy(x=>新{x.讲座,x.教师})
.Select(x=>newcustomtype{teachment=x.Key.Teacher,Teacher=x.Key.Teacher});
}
public class CustomType 
{
   public string Lecture {get; set; }
   public string Teacher {get; set; }
}
public IEnumerable<CustomType> MyMethod()
{
    return classAttendanceByCriteria.OrderBy(x => x.Lecture.Id)
       .GroupBy(x => new { x.Lecture, x.Teacher })
       .Select(x => new CustomType { Lecture = x.Key.Lecture, Teacher = x.Key.Teacher });
}