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_List - Fatal编程技术网

C# 如何编写其答案包含列表的LINQ查询?

C# 如何编写其答案包含列表的LINQ查询?,c#,linq,list,C#,Linq,List,我有StudentDB,它只是一个Student对象列表,定义如下: public class Student { public String StudentID { get; set; } public int ExtraCredit { get; set; } public int QtyDemerits { get; set; } public List<Demerits

我有StudentDB,它只是一个Student对象列表,定义如下:

 public class Student
        {
            public String StudentID { get; set; }
            public int ExtraCredit { get; set; }
            public int QtyDemerits { get; set; }
            public List<Demerits> LstDemerits = new List<Demerits>();
        }

  public class Demerits
        {
            public String TeacherID { get; set; }
            public DateTime AsOf { get; set; }
            public String Description1 { get; set; }
            public String Description2 { get; set; }
        }
公共班级学生
{
公共字符串StudentID{get;set;}
public int ExtraCredit{get;set;}
公共int{get;set;}
public List lstdemorits=新列表();
}
公众阶级的缺点
{
公共字符串TeacherID{get;set;}
公共日期时间AsOf{get;set;}
公共字符串描述1{get;set;}
公共字符串描述2{get;set;}
}
我想编制一份学生名单,按信用外的降序排列,并查看学生的所有记过详细信息(如教师ID、AsOf、说明1和2)

这是我的问题,它并没有产生我想要的一切:

List<Student> StudentDB2 = new List<Student>();

 StudentDB2 = StudentDB.OrderByDescending(x => x.ExtraCredit).Select(r => new Student
        {
            StudentID = r.StudentID,
            ExtraCredit = r.ExtraCredit,
            QtyDemerits = r.QtyDemerits,
            LstDemerits = r.Demerits.OrderBy(c => c.TeacherID).ThenBy(d => d.AsOf).ToList()
        })
        .ToList();

dataGridView1.DataSource = StudentDB2;
List StudentDB2=newlist();
StudentDB2=StudentDB.OrderByDescending(x=>x.ExtraCredit)。选择(r=>newstudent
{
StudentID=r.StudentID,
ExtraCredit=r.ExtraCredit,
QTYEDERTHS=r.QTYEDERTHS,
LstDemerits=r.dremerits.OrderBy(c=>c.TeacherID).ThenBy(d=>d.AsOf).ToList()
})
.ToList();
dataGridView1.DataSource=StudentDB2;

当我运行代码时,我只看到了3列数据:StudentID、ExtraCredit和qtyde。但是如果qtydemertives>0,我就看不到应该显示教师Id和截止日期的lstDemerities中的数据。我的查询有什么问题?

也许你应该尝试筛选所有有缺点的学生。计数>0,类似于:

List<Student> StudentDB2 = new List<Student>();

 StudentDB2 = StudentDB.Where(i => i.LstDemerits.Count > 0).OrderByDescending(x => x.ExtraCredit).Select(r => new Student
        {
            StudentID = r.StudentID,
            ExtraCredit = r.ExtraCredit,
            QtyDemerits = r.QtyDemerits,
            LstDemerits = r.Demerits.OrderBy(c => c.TeacherID).ThenBy(d => d.AsOf).ToList()
        })
        .ToList();

dataGridView1.DataSource = StudentDB2;
List StudentDB2=newlist();
StudentDB2=StudentDB.Where(i=>i.LstDemerits.Count>0)。OrderByDescending(x=>x.ExtraCredit)。选择(r=>newstudent
{
StudentID=r.StudentID,
ExtraCredit=r.ExtraCredit,
QTYEDERTHS=r.QTYEDERTHS,
LstDemerits=r.dremerits.OrderBy(c=>c.TeacherID).ThenBy(d=>d.AsOf).ToList()
})
.ToList();
dataGridView1.DataSource=StudentDB2;
这样你应该避免空列表,这可能是你最后的问题。
我希望这会有所帮助,
lstdemrits
属性没有绑定到数据源,因为它是一个
列表,而不是字符串/int等

尝试将LST缺点设置为如下字符串:

public class Student
{
    public String StudentID { get; set; }
    public int ExtraCredit { get; set; }
    public int QtyDemerits { get; set; }
    public string LstDemerits { get; set; }
}
然后编写自定义逻辑以正确地将
列表
呈现为字符串

StudentDB2 = StudentDB.OrderByDescending(x => x.ExtraCredit).Select(r => new Student
        {
            StudentID = r.StudentID,
            ExtraCredit = r.ExtraCredit,
            QtyDemerits = r.QtyDemerits,
            LstDemerits = CustomLogic(r.Demerits)
        })
        .ToList();

问题不在于您的查询,而在于您将查询绑定到的
GridView
。默认情况下,
gridview
显示绑定到它们的对象的属性,但不能显示类似于
列表的复杂属性。这是合理的,因为试图将
列表
显示为列会导致难以回答的问题。以您自己的对象为例:您将
列表
绑定到
网格视图
,并假设
网格视图
,然后为每个
学生的
l缺点
中的每个
缺点创建四个附加列(每个字段一列)。那可能行得通。但是如果
缺点
是由多个教师分配的,那么
TeacherID
字段本身就是一个
列表
,该怎么办呢。然后,
GridView
可能会为该内部对象列表中的每个
TeacherID
添加额外的列。总的来说,这很快就会变得站不住脚

给出了一个解决方案作为问题的答案。另一个解决方案是在代码隐藏中自己构建一个
数据表
,以便正确显示对象。也许最简单的解决方案是将
列表
对象推入
列表
,其中
s为字段名,
s为类中的数据,使用附加的
-
对,用于
学生
缺点
中的每个
缺点
对象。但是,如果您想对网页执行任何操作而不是显示您的信息,这种方法会带来明显的缺点


在我看来,如果有一个辅助页面,这个信息会更好地表现出来——主页面会显示每个
学生
以及他们的
缺点的数量,在主页面上选择一个
学生
,将带您进入一个显示所有
学生
缺点

的辅助页面,结果是您在网格中看到的结果吗?或者你是否有一个断点来查看StudenDB2包含什么?这是数据网格中显示的结果。但我想查看所有学生是否有缺点。我不想不必要地排除记过数为0的学生。谢谢