Method terminates its execution unexpectedly when calling another method

0

I'm implementing a method that calls a method that should return a IEnumerable<TEntidade> . However, upon returning from this list the method that initiated the call simply terminates its execution without continuing to read the following code.

By debugging, I can access the value of the query variable within the scope of the List method.

Below is an example code. After the line var x = Listar(); the code finishes its execution.

public void DoSomething()
{
      //Some code here
      var x = Listar();
      //Some code here
}


public IEnumerable<ModelEntities> Listar()
{
    var _c = new SMSEntities();
    var query = //query Linq
               select new ModelEntities
               {
                     //inicializando campos
               };
    return query;
}
    
asked by anonymous 11.03.2015 / 14:50

1 answer

2

This does not return IEnumerable<ModelEntities> :

var query = //query Linq
           select new ModelEntities
           {
                 //inicializando campos
           };
return query;

This returns a IQueryable . The return does not even execute the query itself, and that's why you do not see any errors.

To return IEnumerable you need to change return :

return query.ToList();
    
11.03.2015 / 15:51