简体   繁体   English

使用Entity Framework从数据库检索数据并返回到文本框

[英]Retrieve data from database and return into text box using Entity Framework

using (var db = new CrabContext())
{
    var query = db.Reading.Select(s => new
                                       { s.Left1 })
                          .Select(x => x.ToString());
    t1.Text = query;
    //t1.Text == db.Reading.Select(s => new { s.Left1 });
}

I am here trying to retrieve data from database as per mentioned above but my t1.Text is not able to retrieve the data yet I get an error like 我在这里尝试按照上面提到的从数据库中检索数据,但是我的t1.Text无法检索数据,但是出现类似

Can't implicitly convert type 'System.Linq.IQueryable ' to 'String' 无法将类型'System.Linq.IQueryable'隐式转换为'String'

It's because the .Select() returns an IQueryable , an IQueryable is not accessible because the type of the data is not specified so it's only accessible when compiling, if you want to have the value directly prefer to use FirstOrDefault you can pass a lambda expression inside to retrieve a specific object. 这是因为.Select()返回一个IQueryable ,一个IQueryable无法访问,因为未指定数据的类型,所以编译时只有访问,如果你想拥有的价值直接喜欢使用FirstOrDefault你可以通过lambda表达式在内部检索特定对象。

using (var db = new CrabContext())
{
    var query = db.Reading.FirstOrDefault();
    t1.Text = query.Left1;
}

But with the solution I provide the call make to the Db is SELECT * FROM Reading and then you access the property Left1 With IQueryable the query is SELECT Left1 FROM Reading but you can't access the property... Another solution will be to call .toList() at the end of your query... 但是通过我提供的解决方案,对Db的调用是SELECT * FROM Reading ,然后访问属性Left1。使用IQueryable ,查询是SELECT Left1 FROM Reading但您无法访问该属性。另一种解决方案是调用.toList()在查询末尾...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM