0

I am trying to do the following: http://msdn.microsoft.com/en-us/library/dd456857.aspx

I created the function in the edmx file here just before the schema element:

<Function Name="YearsSince" ReturnType="Edm.Int32">
  <Parameter Name="date" Type="Edm.DateTime" />
  <DefiningExpression>
    Year(CurrentDateTime()) - Year(AppliedDate)
  </DefiningExpression>
</Function>
      </Schema>

Now, I want to be able to use that in a query. I created the following code in the ApplicantPosition partial class

[EdmFunction("HRModel", "YearsSince")]
        public static int YearsSince(DateTime date)
        {
            throw new NotSupportedException("Direct calls are not supported.");
        }

And I am trying to do the following query

public class Class1
{

    public void question()
    {
        using (HREntities context = new HREntities())
        {
            // Retrieve instructors hired more than 10 years ago.
            var applicantPositions = from p in context.ApplicantPositions
                              where YearsSince((DateTime)p.AppliedDate) > 10
                              select p;

            foreach (var applicantPosition in applicantPositions)
            {
                Console.WriteLine(applicantPosition.);
            }
        }

    }
}

The YearsSince is not recognized, the MSDN tutorial does not show exactly where I need to put the functio, so that might be my problem.

1 Answer 1

1

Your static YearsSince function must be defined in some class so if it is not Class1 you must use full identification with class name to call it. Check also this answer.

Sign up to request clarification or add additional context in comments.

Comments

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.