score:1
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();
Source: stackoverflow.com
Related Query
- Variable assignment to first item in IEnumerable does not work
- An item in IEnumerable does not equal an item in List
- C# HashSet union on IEnumerable within LINQ Func expression does not work (possible precompiler bug)
- The data source does not support server-side data paging
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- C# 6 null conditional operator does not work for LINQ query
- Order by does not work with Concat() in LINQ
- IEnumerable Concat Missing, does not contain a definition for 'Concat'
- Why does using anonymous type work and using an explicit type not in a GroupBy?
- LINQ: why does this query not work on an ArrayList?
- My Enumerable class does not work with Linq statements like .where in c#
- Nhibernate LINQ DateTime.AddDay does not work
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Most elegant way to process first IEnumerable item differently
- Linq :DataTable select does not work if column name has space in it?
- LINQ Except() Method Does Not Work
- LINQ to Entities Include() does not work
- Why Linq Prepend() does not work with List<T>?
- LINQ - dynamic orderby clause does not work
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- Why does .Equals not work in this LINQ example?
- Escaping the escape character does not work – SQL LIKE Operator
- LINQ query for finding one item in list AND verifying list does not contain another item
- How does linq Expression<TDelegate> assignment work on a language syntax level
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- Why does LINQ not work to add DataRows to a DataTable?
- Multiple queries in async/await (Error: IEnumerable does not contain ToListAsync())
- Why does DbFunctions not work with Linq to Entities
- Linq OrderBy int property does not work
More Query from same tag
- How do I use Linq to query nested dynamic BsonDocuments?
- LINQtoSQL: Query to return List<String>
- Query value if it is not zero using Entity Framework
- Add property to an IEnumerable from property comparison between IEnumerable's
- How do I put business logic in my ADO.NET Entity Framework classes?
- Filter Stored Procedure Results using Linq
- Looking for a better way to get a collection of items by ID
- LINQ: Inner join to the First row in a sub query?
- Filtering Duplicate items in an array based on child array in c#
- Different Ways to Pull Number From Results View Of LINQ Query
- LINQ casting during enumeration
- Int.Parse(String.Split()) returns "Input string was not in a correct format" error
- Create all combinations of two sets
- How to neatly query corresponding object array items?
- Umbraco 7 LINQ to filter Model children on date range
- How to apply multiple joins as like in a SQL query in linq
- Sort a list and all its nested objects using LINQ
- How to get DropDownList text from selected value via Linq
- Will this Post/Tag DB schema cause problems later?
- using Join on List View ASP.NET MVC
- Convert the result of LINQ query to list of Custom Model
- Linq - get unavailable IDs in a list using linq
- C# - Filling List with LINQ from another List<>
- C# LINQ IdentityContext Multi Group Join
- Difference between contains and contains<> in c#
- SQL query with group by and where filtering a received parameter
- Find child objects in list of parent objects using LINQ
- Entity Framework: LINQ query generates different SQL between local execution and server execution
- LINQ (method) returning multiple records
- C# Linq and Regexing non-unicode