我有以下 LINQ to Entities 表达式:
var allItemsOver64 =
_inventoryContext.Items.Where(
i => DateTimeHelper.CalculateAgeInYears(i.PrimaryInsured.DoB, now) >= 65);
问题是,当我使用 allItemsOver64 时,它说不支持此表达式。我感觉这个错误是由于调用了 CalculateAgeInYears 方法而发生的。为什么会发生这种情况,我该如何解决?
谢谢,
萨钦
编辑:
即使将代码更改为使用 IEnumerables,我仍然遇到相同的错误。现在这是我的代码:
DateTime now = DateTime.UtcNow;
var allItemsOver64 =
_inventoryContext.Items.Where(
i => DateTimeHelper.CalculateAgeInYears(i.PrimaryInsured.DoB, now) >= 65).AsEnumerable();
IEnumerable<Item> items65To69 = allItemsOver64.Where(
i =>
DateTimeHelper.CalculateAgeInYears(i.PrimaryInsured.DoB, now) >= 65 &&
DateTimeHelper.CalculateAgeInYears(i.PrimaryInsured.DoB, now) <= 69).AsEnumerable();
最佳答案
您不能在 EF 查询中调用 C# 方法,因为 EF 不知道如何将方法转换为 SQL。
相反,首先调用 .AsEnumerable()
以强制 Where()
在本地运行。
关于c# - 不支持 Linq 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335560/