【发布时间】:2011-11-02 08:43:45
【问题描述】:
我正在尝试执行以下操作: http://msdn.microsoft.com/en-us/library/dd456857.aspx
我在 edmx 文件中在模式元素之前创建了函数:
<Function Name="YearsSince" ReturnType="Edm.Int32">
<Parameter Name="date" Type="Edm.DateTime" />
<DefiningExpression>
Year(CurrentDateTime()) - Year(AppliedDate)
</DefiningExpression>
</Function>
</Schema>
现在,我希望能够在查询中使用它。 我在申请人位置部分类中创建了以下代码
[EdmFunction("HRModel", "YearsSince")]
public static int YearsSince(DateTime date)
{
throw new NotSupportedException("Direct calls are not supported.");
}
我正在尝试执行以下查询
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 无法识别,MSDN 教程没有准确显示我需要放置函数的位置,所以这可能是我的问题。
【问题讨论】:
标签: entity-framework linq-to-entities entity-framework-4.1