I am having a problem with the following LINQ query. When the subquery (item.Items) has no objects, I get the exception "Value cannot be empty. Parameter name: source.".
How can I make an internal query return an empty list of elements in the query?
var items = from item in _repository.GetStreamItems() select new { Title = item.Title, Description = item.Description, MetaData = item.MetaData, ItemType = item.ItemType, Items = from subItem in item.Items select new { Title = subItem.Title, Description = subItem.Description, MetaData = subItem.MetaData, ItemType = subItem.ItemType } };
Here's the same query written using method calls instead of query syntax. Same question:
var items = _repository.GetStreamItems() .Select(x => new { Title = x.Title, Description = x.Description, MetaData = x.MetaData, ItemType = x.ItemType, Items = x.Items.Select(x2 => new { Title = x2.Title, Description = x2.Description, MetaData = x2.MetaData, ItemType = x2.ItemType, Items = x2.Items.Select(x3 => new { Title = x3.Title, Description = x3.Description, MetaData = x3.MetaData, ItemType = x3.ItemType }) }) });
Any ideas how to test or avoid the null item.Items value? I have a feeling that I'm just not enough.
source share