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?
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With