且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将类型'System.Linq.IQueryable'隐式转换为'string'

更新时间:2023-02-18 20:55:33

您可能想要阅读错误消息。它确切地告诉你什么是错的。你试图将一个不是字符串的对象分配给需要字符串的属性(Text)。



你的代码没有任何意义,因为LINQ查询返回一个IQueryable对象(可以返回0或更多结果),你实际上无法将其转换为字符串。


如果你不使用'Single:这会容易得多:
  var  hdd =( from  d  in  db.EMPs 
其中​​ d.Name == JOHN
select d).ToList();

Label1.Text =(hdd.Count == 0 )? 不匹配:hdd [ 0 ]。市;


I am fetching the employee name from DB and displaying it in a label,if the name exists it has to show the name if not it has tell that no record exists.

var hdd = from d in db.EMPs
where d.Name == "JOHN"
select d.City;

Label1.Text = hdd;

But i am getting an error Cannot implicitly convert type 'System.Linq.IQueryable<char>' to 'string'

You might want to read the error message. It's telling you EXACTLY what's wrong. You're trying to assign an object that is not a string to a property (Text) that requires a string.

Your code doesn't make any sense since the LINQ query returns an IQueryable object (0 or more results can be returned) and you really can't convert that to a string.


This is much easier if you do not use 'Single:
var hdd = (from d in db.EMPs
             where d.Name == "JOHN"
               select d).ToList();

Label1.Text = (hdd.Count == 0) ? "No match" : hdd[0].City;