如何在list<>?中编写label.text [英] How to write a label.text in list<>?

查看:82
本文介绍了如何在list<>?中编写label.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

用户user =(来自x   myEnt.Users其中( x.ID == id)&&(x.Title == title)select x).FirstOrDefault(); 

lblTitle.Text = user.Title;





并且它有效完美。但后来我改为:



 List< users> users =(from x  in  myEnt.Users其中(x.ID == id)&&(x.Title == title)选择x).ToList( ); 

lblTitle.Text = users.Title // not working





获取  this 错误:
System.Collections.Generic.List<>不包含定义 ' Title'并且没有扩展方法' 标题'接受类型' System.Collections.Generic.List<>可以找到(你错过了使用指令或汇编参考吗?)





我试过的:



lblTitle.Text = users.Title //错误消息



lblTitle。 Text = Title //没有错误信息,只是空的。



lblTitle.Text = Title.ToString()//没有错误信息,只是空。

解决方案

用户是一个列表,没有名为 Title 的属性。 ..(标题是单个用户的属性)

要从列表中的所有元素获取相同的属性,您可以使用LINQ,如下所示:

 lblTitle.Text =  string  .Join( ,users.Select(oUser = >  oUser.Title)。ToArray()); 


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?) 



What I have tried:

lblTitle.Text = users.Title // error message

lblTitle.Text = Title // no error message, just empty.

lblTitle.Text = Title.ToString() // no error message, just empty.

解决方案

users is a list and has no property named Title... (Title is a property the single user)
To get the same property from all the elements in the list you can use LINQ, like this:

lblTitle.Text = string.Join(", ", users.Select(oUser => oUser.Title).ToArray());


这篇关于如何在list&lt;&gt;?中编写label.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆