0

I have a entity object in c#. It queries a database table called FBApi. The table stores an integer year and integer month. I have function that needs to return all records greater than the passed in parameters.

public query(int myYear, int myMonth){
   var context = new MCSSUtility.Entities();
   return context.FBApis.Where(p => p.month == month && p.year == year);
}

I was thinking of converting the integers to a DateTime object, but i am not sure how to dynamically convert the where clause to Datetime variable?

public query(int myYear, int myMonth){
   DateTime my = new DateTime(myYear,myMonth,1);
   var context = new MCSSUtility.Entities();
   return context.FBApis.Where(p => new DateTime(p.year,p.month,1) >= my);
}
2
  • what do you want to return? You have to include the retrun type of the method (datetime or bool?). Commented Sep 4, 2012 at 17:28
  • Its just a quick example I created. Return isn't important. Thanks though. Commented Sep 4, 2012 at 17:41

2 Answers 2

3

Please try this one:

            public query(int myYear, int myMonth){
               DateTime my = new DateTime(myYear,myMonth,1);
               var context = new MCSSUtility.Entities();
               return context.FBApis.Where(p => EntityFunctions.CreateDateTime(p.year, p.month, 1, 0, 0, 0)  >= my);
            }
Sign up to request clarification or add additional context in comments.

Comments

0

Try first selecting an anonymous object with two properties: your FBApi object and your constructed datetime:

return context.FBApis
.Select(s=>new{MyFBAPIObject=s,MyDateTime=newDateTime(s.Year,s.Month,1)})
.Where(w=>w.MyDateTime>=my)
.Select(s2=>s2.MyFBAPIObject);

Comments

Your Answer

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