LeBlues
LeBlues

Reputation: 301

Linq with data Error condition ( 'Date' is not supported in LINQ)

I am using a LINQ statement to get some data from the table based on data value:

var oDay = diabeticDB.user_data
                     .Where(y => y.date >= DateTime.Now.Date
                            & y.BG != null
                            & y.userid == u)
                     .Average(x => x.BG).Value;

I get the following error:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Upvotes: 1

Views: 215

Answers (1)

Charles Mager
Charles Mager

Reputation: 26223

The expression contained in Where(...) is converted to SQL, and the EF query provider (that does this conversion) does not understand DateTime.Now.Date.

The expression can contain a specific value, though - so create a local variable for today's date and use that:

var today = DateTime.Now.Date;

var oDay = diabeticDB.user_data
    .Where(y => y.date >= today & y.BG != null & y.userid == u)
    .Average(x => x.BG).Value;

Upvotes: 4

Related Questions