0
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'

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

If you're using FirstOrDefault, then it's better to include null checking so that OP would understand that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.