Reputation: 9930
Right off the bat I'm very new to LINQ to SQL (and C# in general) and haven't successfully implemented .Any() before. I'm using .Net 4.0 with Visual Studio 2010.
I've added a new LINQ to SQL data set to my project and I'm trying to do the following:
var db = new HealthIndicatorsDataContext();
var d = DateTime.Today;
if(db.HealthIndicators.FirstOrDefault(h => h.LogDate == d).Any())
// do something
Unfortunately I'm receiving Cannot Resolve Sysmbol 'Any'
and .Any() is highlighted in red. I've read that I needed to reference System.Core.dll in my project, I've added this but I'm still having no luck. What am I missing?
Upvotes: 2
Views: 449
Reputation: 1547
FirstOrDefault() does not return a collection so Any() does not make sense. You can replace FirstOrDefault() with .Any() if you only need to know if at least one exists.
if(db.HealthIndicators.Any(h => h.LogDate == d))
// do something
Upvotes: 6
Reputation: 161012
if(db.HealthIndicators.FirstOrDefault(h => h.LogDate == d).Any())
FirstOrDefault()
returns a single item and not an IQueryable
or IEnumerable
- Any()
is only defined on those. What you probably meant to write is:
if(db.HealthIndicators.FirstOrDefault(h => h.LogDate == d) != null))
or (better):
if(db.HealthIndicators.Any(h => h.LogDate == d)))
Upvotes: 7