我只是想查询只返回与eg FirstName相关联的gridview模块

本文关键字:关联 gridview 模块 FirstName 查询 返回 eg | 更新日期: 2023-09-27 17:53:54

我只需要查询返回Student Firstname等于说"ceaer"的模块

谢谢你,我早些时候发布了其中一个,但显然我可能误导了你们。它只是我想从下面的查询返回的模块。

List<Student> arrList = new List<Student>();
    //   ArrayList arrList = new ArrayList();
        arrList.Add(
            new Student
            {
                FirstName = "Svetlana",
                LastName = "Omelchenko",
                Password = "hh",
                modules = new string[] { "001", "002", "003", "004" }
            });
        arrList.Add(
            new Student
            {
                FirstName = "Claire",
                LastName = "O’Donnell",
                Password = "hh",
                modules = new string[] { "001", "002", "003", "004" }
            });
        arrList.Add(
            new Student
            {
                FirstName = "Sven",
                LastName = "Mortensen",
                Password = "hh",
                modules = new string[] { "001", "002", "003", "004" }
            });
        arrList.Add(
            new Student
            {
                FirstName = "Cesar",
                LastName = "Garcia",
                Password = "hh",
                modules = new string[] { "HH", "KK", "LL", "OO" }
            });

        var result = from student in arrList
                     where student.FirstName == "Cesar"
                     select student.modules;
        GridView1.DataSource = result.ToList();

        At the moment, this is what the query returns
        Length | Long Length | Rank | IsReadOnly | IsFixedSize.,........
          4           4          1

我只是想查询只返回与eg FirstName相关联的gridview模块

代码中的linq查询返回IEnumerable<string[]>,因为您选择的是modules属性的集合,这是string[]

如果你想显示模块数组本身(只是一个学生的模块),你可以改变查询逻辑一点:

var student = (from student in arrList
               where student.FirstName == "Cesar"
               select student).FirstOrDefault();
if(student != null)
    GridView1.DataSource = student.modules;