0

I have a table of events, I need a linq query that will return all events from a certain month and year, not day.

something like

var monthquery = from c in db.events
                  where c.startdate.month == DateTime(MonthValue, Year Value)
                  select c;

I just don't know the syntax. What do I put in instead of MonthValue to get July, for instance?

Thanks in advance

1 Answer 1

3

Instead of MonthValue, you can put the integer that represents the month ...

var monthquery = from c in db.events
                  where c.startdate.month == IntegerThatRepresentTheMonth 
                  && C.startDate.Year == IntegerThatRepresentTheYear
                  select c;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I'm only beginning this lark, your help is appreciated!
Could I pick your brain further? I have two functions to set the month and year and I'v replaced the integer values with these functions, but I'm getting this error ... LINQ to Entities does not recognize the method 'Int32 GetMonth()' method, and this method cannot be translated into a store expression.... Any idea how I use a function in a linq query?
Sorry for the later. We're very busy in my officce. Well...I think you can't use this kind of custom functions inside a Linq Query. Just call the functions before and store the result inside a var. Then, in the linq query, compare against this var. In addition, this two more lines of code will let you read and debug your app in an easy way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.