【问题标题】:Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL方法“System.Object DynamicInvoke(System.Object[])”不支持 SQL 转换
【发布时间】:2011-06-03 19:47:33
【问题描述】:

假设我有一个名为 Poll 的表,我想编写一个 LINQ 扩展来列出所有 ID 属于数组的轮询。例如:

void Main()
{
    long[] ids = new long[]{ 1,2,3,4,5,6,7,8,9 };

    ListFail<Poll>(Poll, p => p.ID, ids); //Failed!
    ListOK<Poll>(Poll, p => p.ID, ids); //OK!
}

public void ListFail<T>(Table<T> obj, Func<T, long> idProperty, long[] ids) where T : class
{
    obj.Where(p => ids.Contains(idProperty(p))).ToList().Dump();
}

public void ListOK<T>(Table<T> obj, Func<T, long> idProperty, long[] ids) where T : class
{
    obj.ToList().Where(p => ids.Contains(idProperty(p))).ToList().Dump();
}

我不知道为什么我在ListFail 上收到错误消息

Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.

当我在Where 之前添加ToList() 时,它在ListOK 上运行良好,但我当然不想得到整个Poll 表。

有什么想法吗?

【问题讨论】:

    标签: c# linq entity-framework dynamic-invoke


    【解决方案1】:

    Func&lt;T, long&gt; 更改为Expression&lt;Func&lt;T, long&gt;&gt;。目前,正因为如此,EF 不明白。

    【讨论】:

    • 我已根据您的建议进行了更改,但出现了另一个错误'idProperty' is a 'variable' but is used like a 'method'
    • 对不起。我帮不了你。查询组合有时可能很糟糕,这看起来并不容易实现。想到的一件事是手动创建表达式树。但我没有时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多