dblankman
asked on
How to write a select year(max(datefield) in EntitySQL in c# entity framework
I have the following sql query:
SELECT year(max(eventdate)
FROM TurtleEvent
How do I write this in EntitySQL
SELECT year(max(eventdate)
FROM TurtleEvent
How do I write this in EntitySQL
ASKER
I am getting the date from a database query. Normally I would do something like
ste is a reference to entities
var varDate =( from d in ste.TurtleEvent
select d)
however, that gets me a whole row. How do I populate the list you propose from a database query where the date field is a sql datetime?
ste is a reference to entities
var varDate =( from d in ste.TurtleEvent
select d)
however, that gets me a whole row. How do I populate the list you propose from a database query where the date field is a sql datetime?
Hi,
Pls try this..
Hope it helps!
Pls try this..
TurtleEvent.Eventdate.MAX().ToString("YYYY")
Hope it helps!
That would be:
DateTime maxDate = (from event in ste.TurtleEvent select event.eventdate).Max();
or:int year = (from event in ste.TurtleEvent select event.eventdate).Max().Year;
/gustav
ASKER
When I try either of these I get an error saying that "from" does not exist in this context. It seems that the "from" syntax only works with a var.
Hi,
Any luck with mine?
Thank you.
Pawan
Any luck with mine?
Thank you.
Pawan
Then try with the Lambda syntax:
/gustav
int year = ste.TurtleEvent.Select(d => d.Year).Max();
Or perhaps something else is going on./gustav
ASKER
Pawan, in what context would this go. TurtleEvent.Eventdate.MAX( ).ToString ("YYYY"). ?
Hi,
Pls try like..
Hope it helps!
Pls try like..
int year = ste.TurtleEvent.Eventdate.MAX().Year;
Hope it helps!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, that helps a lot.
You are welcome!
/gustav
/gustav
Open in new window
Or, with no Lambda:Open in new window
/gustav