Reputation: 4517
Yet again, I've come up against a LINQ issue I don't understand.
So I thought I'd better ask a generic question so maybe next time I can figure it out for myself.
What is wrong with this statement?
var cells = settingsSheet.Range["B1:B999"].Select(x => x.Value2 != null);
Forget the fact that it's an Excel range, I want to know why I can't just query it in this way. Thinking it might be a Type issue I've tried the following and this doesn't work either:
var cells = settingsSheet.Range["B1:B999"].ToList<Excel.Range>().Select(x => x.Value2 != null);
When I say doesn't work, the word Value2 appears in red ("Cannot resolve symbol Value2").
Value2 is a valid property of the Range class.
Why can't I do it this way?
To clarify, the following works fine:
var cells = settingsSheet.Range["B1:B999"];
foreach (var q in cells)
{
a = q.Value2);
}
Upvotes: 1
Views: 147
Reputation: 4517
After I had strongly-typed my settingsSheet variable, this is what worked:
var cells = settingsSheet.Range["B1:B999"].Cast<Excel.Range>().Select(x => x.Value2 != null);
So, the result of Range[]
must be explicitly cast to a Range
type.
Although there is a push towards implicit typing, explicit typing is needed for querying with LINQ
Upvotes: 1