无法将类型'System.Linq.IQueryable'隐式转换为'string' [英] Cannot implicitly convert type 'System.Linq.IQueryable' to 'string'

查看:884
本文介绍了无法将类型'System.Linq.IQueryable'隐式转换为'string'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从DB获取员工姓名并将其显示在标签中,如果名称存在,则必须显示姓名,如果没有,则表示没有记录。



var hdd =来自db.EMP中的d

其中d.Name ==JOHN

选择d.City;



Label1.Text = hdd;



但我收到错误无法隐式转换类型'System.Linq.IQueryable< char> 'to'string'

解决方案

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



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


如果你不使用'Single:这会容易得多:

  var  hdd =( from  d  in  db.EMPs 
其中​​ d.Name == < span class =code-string> 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;


这篇关于无法将类型'System.Linq.IQueryable'隐式转换为'string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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