Click here to Skip to main content
14,971,215 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to choose select combobax item.And show treeview item.Treeview item has is checkbox and textbox.Refers to the textbox namespace.
If there are many students, checkbox should come up according to the value of combobax we selected.According to the following method, I'm waiting for the query to come to the checkbox.
For example: is combobax item and database is has three numbers.I want to 3 three checkbox and name,id.

checkbox figure 1 John
checkbox figure 2 Mary
checkbox figure 3 Martin

What I have tried:

public void Student(){
var query= (from p in tableA join b in tableB on p.ID equals b.ID select new { ID=p.ID,Name=p.Name}).ToList();
foreach(var a in query)
{
Student stud=new Student();
stud.IDS=a[ID].ToString();
stud.Names=a[Names].ToString().Trim();
studList.Add(stud);
}


a[ID] and a[Names] is error.
This error is:
Cannot apply indexing with [] to an expression of type 'Anonymous Type#1'


}
Posted
Updated 18-Oct-19 4:50am
v3

You are using anonymous types and there are limits as to what you can do with them, so the solution is to not use them.

Create a concrete class with the appropriate ID and Name properties

class MyData
{
    public int ID { get; set; }
    public string Name { get; set; }
}


update your query to use it

var query= (from p in tableA join b in tableB on p.ID equals b.ID select new MyData { ID=p.ID,Name=p.Name}).ToList();
   
Unlike Javascript, in C# you can't use the indexer to access properties of an object. You need to use the properties directly:
C#
stud.IDS = a.ID.ToString();
stud.Names = a.Name.ToString().Trim();
NB: I suspect the Name property is already a string, so you don't need to call Name.ToString().
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900