I have written a very simple windows application using .Net Core which produces an IEnumerable with 1000 records. In the following code which is shown as an example, my IEnumerable return the correct records while the second line of code return null.
IEnumerable<test> myList = null;
myList = from item in myTable select ....();
var result = myList.ToList(); //myList has 1000 records
int total = myList.Count(); //myList is null
I don't understand why myList is not persisted and is changed to null. It looks like it is flushed after being used once.
Please see the real code below:
using var dbContextTransation = _context.Database.BeginTransaction();
try
{
IEnumerable<BuoyLocation> positionList = null;
var allPosition = csvReader.GetRecords<MyFormat>();
positionList =
from location in allPosition
select new Location()
{
LocationDateTime = location.Fecha.Add(new TimeSpan( Convert.ToInt32( location.Hora.Left(2) ), Convert.ToInt32( location.Hora.Right(2) ), 0 ) ),
Latitude = location.Latitud,
Longitude = location.Longitud
};
var result = positionList.ToList();//List has 1000 records
int total = positionList.Count(); //List is null
....
dbContextTransation.Commit();
// _context.Dispose();
Console.WriteLine("Committed " + DateTime.Now);
}
catch (Exception ex)
{
dbContextTransation.Rollback();
LogService.LogWrite(textFile, "ERR:" + ex.Message + " ->" + ex.InnerException.Message);
return 0;
}
Thank you