Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use .Any()?

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?

like image 319
Michael A Avatar asked Jan 17 '26 04:01

Michael A


1 Answers

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)))
like image 61
BrokenGlass Avatar answered Jan 19 '26 16:01

BrokenGlass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!