Answered by:
Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values

Question
-
User-343630552 posted
I have a simple Linq query containing: .Where(x => x.Year == year), where year = 2018. It is against a List<T> that only has 8 elements, all of which have a valid entry in the Year field. Only one of the List items has Year == 2018. But the returned result, which I convert to a list via ToList(), contains 4 elements, including the one with the matching Year and 3 others that show as null. I am at a loss to explain this behavior. Any ideas? The Where query is in a Blazor Wasm component being developed with VS Community 2019, if that makes any difference. Thanks. Steve
Monday, July 6, 2020 11:27 PM
Answers
-
User771289253 posted
I think this is just a debugging issue. Because when I log the length of the list it logs the correct length without including the null objects. Just when I try to inspect the collection inside visual studio it shows the nulls
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 20, 2020 2:50 PM
All replies
-
User475983607 posted
The community will need sample code that reproduces this issue.
public class Data { public int Year { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { List<Data> results = PopluateTestData().Where(m => m.Year == 2018).ToList(); Console.WriteLine(results.Count()); } public static List<Data> PopluateTestData() { List<Data> list = new List<Data>(); for (int i = 0; i < 8; i++) { list.Add(new Data() { Year = 2012 + i, Name = $"Item {i}"}); } return list; } }
Results
1
Tuesday, July 7, 2020 12:00 AM -
User-343630552 posted
I have not been able to recreate the problem in a demo app. While I suspect finding an answer will be elusive, here are the circumstances that led up to the Linq Where statement. I also cannot take screenshots of the variables since I can only see them in the VS debugger whose popups aren't captured in a screenshot. I apologize for the complexity of all this. It is part of a demo app where I'm trying to learn how these capabilities work and work together.
As I said originally, the app is a Blazor Wasm one developed in Visual Studio Community 2016. The data that's in the source List<T> comes from an IndexedDB database object that is read via a Javascript function that's invoked when a button is clicked. This object is passed to a Blazor component via DotNet.InvokeAsync with the object as a parameter. The Blazor component disassembles the object into various parts including a full List<T>, which is then filtered via a Where clause. The List<T> is in a Blazor Services data class that enables data sharing across Blazor components. At this point in the execution, everything looks as I would expect in the VS debugger.
Based on a 2nd button click that invokes another Blazor method, the List<T> in the Services class is assigned to a local List<T>. This is then assigned to a different local List<T> after filtering with another Where clause. It's this final Where that returns 4 items, 3 of which show in the VS debugger as null.
I'm guessing I just needlessly spent the time to describe this because we can't replicate it in a simpler demo app. Anyway, at least it's in the forum in case anyone else experiences something similar.
Wednesday, July 8, 2020 12:57 AM -
User771289253 posted
I'm experiencing the same issue. Were you able to find out something more?
Friday, November 20, 2020 1:35 PM -
User-343630552 posted
Regrettably NO.
Friday, November 20, 2020 2:48 PM -
User771289253 posted
I think this is just a debugging issue. Because when I log the length of the list it logs the correct length without including the null objects. Just when I try to inspect the collection inside visual studio it shows the nulls
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 20, 2020 2:50 PM