Get data using LINQ and show result in a gridview

后端 未结 3 1463
陌清茗
陌清茗 2021-01-27 04:25

How can I get all modules from the array like below where student firstName, using Linq in C# and Asp.net. It\'s something probably easy to do but have failed to get the trick.

相关标签:
3条回答
  • 2021-01-27 04:30

    you are missing ToList at the end of your query.

    yourGridView.DataSource = (from Student s in arrList
                        where s.FirstName == "Cesar"
                        select s).ToList();
    

    then bind it to your gridview.

    0 讨论(0)
  • 2021-01-27 04:33

    You should avoid ArrayLists. Use List instead.

    List<Student> StudentList = new List<Student>(); /* ... */
    

    And you query should look like:

    var query = from student in StudentList
                where student.FirstName == "Cesar"
                select  student;
    

    Then bind your Grid:

     GridView1.DataSource = query.ToList();
     GridView1.DataBind();
    
    0 讨论(0)
  • 2021-01-27 04:35

    Consider using a List<Student> instead:

    List<Student> list = new List<Student>();  
        list.Add(
           // ... Just like in your OP
         });
    

    Then get the result:

    var result = from student in list
                 where student.FirstName == "Cesar"
                 select  student;
    

    You should then be able to add the result to your datasource using ToList() on the returned IEnumerable:

     gridView.DataSource = result.ToList();
    
    0 讨论(0)
提交回复
热议问题