Performing a query C#

Asked

Viewed 95 times

1

I have a controller where I make a Select to get a list, but it returns empty.

public IEnumerable<Check> GetUserByNumberOfregistration(int numberOfregistrationUser)
{
    CheckContext contextObj = new CheckContext();

    var result = contextObj.hour
        .Where(x => x.NumberOfregistrationUser == numberOfregistrationUser)
        .Select(p => new Check()
        {
            Id = p.Id,
            ArrivalTime = p.ArrivalTime,
            DepartureTime = p.DepartureTime,
            NumberOfregistrationUser = p.NumberOfregistrationUser
        });

    return result;
}
  • It’s hard to know why it has to be this code.

  • '((System.Data.Entity.Infrastructure.Dbquery<Appteste.Models.Check>)result). Sql' threw an Exception of type 'System.Notsupportedexception'

  • @Gvr Managed to solve now?

1 answer

1


This is happening because your result is of the type dbquery. You have to execute the query after Where or Select using the method ToList() to return a collection, or the method FirstOrDefault() to return only one object.

In your case, it would look like this:

public IEnumerable<Check> GetUserByNumberOfregistration(int numberOfregistrationUser)
{ 
    CheckContext contextObj = new CheckContext();
    var result = contextObj.hour
                           .Where(x => x.NumberOfregistrationUser == numberOfregistrationUser) 
                           .Select(p => new Check() { Id = p.Id, ArrivalTime = p.ArrivalTime, DepartureTime = p.DepartureTime, NumberOfregistrationUser = p.NumberOfregistrationUser })
                           .ToList(); 
    return result;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.