Conducting a query linq C #

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;
}
    
asked by anonymous 06.02.2018 / 00:53

1 answer

2

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

For 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;
}
    
06.02.2018 / 02:06