user7404945
user7404945

Reputation:

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

Can't implicitly convert type 'System.Linq.IQueryable ' to 'String'

Upvotes: 0

Views: 539

Answers (1)

Max
Max

Reputation: 862

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.

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...

Upvotes: 3

Related Questions