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
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;
}
}
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.
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
Member
26 Points
63 Posts
Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Jul 06, 2020 11:27 PM|CincySteve|LINK
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
All-Star
53001 Points
23587 Posts
Re: Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Jul 07, 2020 12:00 AM|mgebhard|LINK
The community will need sample code that reproduces this issue.
Results
Member
26 Points
63 Posts
Re: Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Jul 08, 2020 12:57 AM|CincySteve|LINK
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.
Member
10 Points
2 Posts
Re: Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Nov 20, 2020 01:35 PM|Daniel Glos|LINK
I'm experiencing the same issue. Were you able to find out something more?
Member
26 Points
63 Posts
Re: Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Nov 20, 2020 02:48 PM|CincySteve|LINK
Regrettably NO.
Member
10 Points
2 Posts
Re: Linq 'Where' Query Returning One Valid Result Plus Multiple Null Values
Nov 20, 2020 02:50 PM|Daniel Glos|LINK
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