Jana Pasch Jana Pasch - 1 year ago 163
C# Question

How to write a label.text in List<>?

I had this Code:

Users user = (from x in myEnt.Users
where (x.ID == id) && (x.Title == title)
select x).FirstOrDefault();

lblTitle.Text = user.Title;

and it worked perfectly. But then I changed it to:

List<Users> users= (from x in myEnt.Users
where (x.ID == id) && (x.Title == title)
select x).ToList();

lblTitle.Text = users.Title // not working

I get this error:

System.Collections.Generic.List<> does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'System.Collections.Generic.List<> could be found (are you missing a using directive or an assembly reference?)

Answer Source

You have multiple values and one text box, you might need to have them comma separated:

lblTitle.Text = String.Join(", ", users.Select(x=> x.Title));

The errors states that users is a List and it doesn't have a property called Title. This is self descriptive, each element inside that list has a property called Title. That's why we select that Title out of each element and sparate them with a comma, build a string out of that and assign that string to the Text property of that Label control.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download