Reputation: 37
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. Please help. Thanks.
ArrayList arrList = new ArrayList(); //new array list for students
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",
modules = new string[] { "001", "002", "003", "004" }
});
arrList.Add(
new Student
{
FirstName = "Sven",
LastName = "Mortensen",
modules = new string[] { "001", "002", "003", "004" }
});
arrList.Add(
new Student
{
FirstName = "Cesar",
LastName = "Garcia",
modules = new string[] { "HH", "KK", "LL", "OO" }
});
var query = from Student student in arrList
where student.FirstName == "Cesar"
select query;
GridView1.DataSource = query;
GridView1.DataBind();
I want to put the results in a grid table in Asp.net. Please help!!
Upvotes: 2
Views: 4413
Reputation: 32719
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.
Upvotes: 2
Reputation: 19151
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();
Upvotes: 0
Reputation: 23945
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();
Upvotes: 1