score:1

Accepted answer
if (toCallers.Any())
{
    toCallers.First().Show = true;
    Console.Log(toCallers.First().Show); //THIS LOGS 'false'. HOWEVER, IT SHOULD LOG 'true'
}

Every time you call .First() you are getting the first item. For some enumerables (e.g. IQueryable) it will return a different object every time.

The below code will call the method only once and thus avoid the issue. Note also that I have used FirstOrDefault rather than Any then First - since the former will result in fewer DB queries (i.e. be faster).

var caller = toCallers.FirstOrDefault().
if (caller != null)
{
    caller.Show = true;
    Console.Log(caller.Show);
}

score:1

var callers = dbCallerRecs.Select(x => new CallerItem() { IndexId = x.IndexId, PhoneNumber = x.PhoneNumber, ToInd = x.ToInd });
var toCallers = callers.Where(x => x.ToInd);

defines a query which is evaluated when some elements in the resulting IEnumerable<CallerItem> (or IQueryable<CallerItem> which implements IEnumerable<CallerItem>) is iterated. This happens three times in your code - when calling Any and both times you call First (assuming .Any() returns true).

The reason you see this behaviour is the two calls to First cause the query to be re-evaluated and a new object to be created for each call, so you're modifying a different object the one you end up logging.

One solution would be to eagerly evaluate the query:

var toCallers = callers.Where(x => x.ToInd).ToList();

Related Articles