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 Articles
- Variable assignment to first item in IEnumerable does not work
- An item in IEnumerable does not equal an item in List
- Most elegant way to process first IEnumerable item differently
- How does linq Expression<TDelegate> assignment work on a language syntax level
- Why did my code work when I changed it from IEnumerable to List?
- C# HashSet union on IEnumerable within LINQ Func expression does not work (possible precompiler bug)
- How does a LINQ IEnumerable work under the hood?
- Why Entity Framework Code First one to many Doesn't work properly
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- Entity Framework Code First Select Item Based on Relationship
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- Linq code to select one item
- Does LINQ work with IEnumerable?
- Updating an item property within IEnumerable but the property doesn't stay set?
- Why does Enumerable.Single() iterate all elements, even when more than one item has already been found?
- LINQ - selecting second item in IEnumerable
- The data source does not support server-side data paging
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- How does LINQ expression syntax work with Include() for eager loading
- Get previous and next item in a IEnumerable using LINQ
- How does PredicateBuilder work
- Update item in IEnumerable
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Entity Framework 6 Code First Custom Functions
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- C# 6 null conditional operator does not work for LINQ query
- does converting IQueryable to IEnumerable execute the query again?
- Order by does not work with Concat() in LINQ
- Does FirstOrDefault return a reference to the item in the collection or the value?
- Running a LINQ query for multiple years
- LINQ syntax for CONTAINS method with an inclusive "filter"
- Get max value of a column and id
- LINQ to retrieve first matching item, or a blank value for each item in a list
- Linq Filtering items with ForEach crashes for large data set
- ASP. NET MVC3 - How to display all Comments in Post
- Executing DynamicExpression with unknown types
- Acces data from objects in a list
- LINQ to Entity Select Query Error
- Problem in c# windows service using linq
- Linq to Sql Datacontext primary key not set
- NotMapped Field not get assigned by values on procedure calling in .NET Core
- LINQ to XML issue due to xml namespace
- linq nested select new
- jQuery UI Ajax Autocomplete case sensitive ASP.net MVC
- Using join throws exception that cannot be debugged
- How can I filter table and select rows which have parentid in id column
- Call method on LINQ query results succinctly
- Linq to Entities count subquery?
- Left Join in Linq with nullable references