【问题标题】:System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.'System.NotSupportedException: 'LINQ to Entities 不支持 LINQ 表达式节点类型'Invoke'。'
【发布时间】:2017-06-25 15:02:22
【问题描述】:

这是我的代码:

    private void loadList(IQueryable<customer> customers)
    {
        ListView.Items.Clear();
        foreach (var customer in customers)
        {
            ListViewItem item = new ListViewItem(customer.customerNumber.ToString());
            item.SubItems.Add(customer.customerName);
            item.SubItems.Add(customer.contactFirstName);
            item.SubItems.Add(customer.contactLastName);
            item.SubItems.Add(customer.phone);
            item.SubItems.Add(customer.addressLine1);
            item.SubItems.Add(customer.addressLine2);
            item.SubItems.Add(customer.city);
            item.SubItems.Add(customer.state);
            item.SubItems.Add(customer.postalCode);
            item.SubItems.Add(customer.country);
            item.SubItems.Add(customer.salesRepEmployeeNumber.ToString());
            item.SubItems.Add(customer.creditLimit.ToString());

            ListView.Items.AddRange(new ListViewItem[] { item });
        }
    }

    private static readonly Func<customer, string>[] _searches;

    static Main()
    {
        _searches = new Func<customer, string>[]
        {
            (c) => c.customerNumber.ToString(),
            (c) => c.customerName,
            (c) => c.contactFirstName,
            (c) => c.contactLastName,
            (c) => c.phone,
            (c) => c.addressLine1,
            (c) => c.addressLine2,
            (c) => c.city,
            (c) => c.state,
            (c) => c.postalCode,
            (c) => c.country,
            (c) => c.salesRepEmployeeNumber.ToString(),
            (c) => c.creditLimit.ToString(),
        };
    }

    protected virtual void SearchBox_TextChanged(object sender, EventArgs e)
    {

        var search = _searches[SearchItem.SelectedIndex];

        CustomerContext context = new CustomerContext();

        IQueryable<customer> customers = from x in context.customers
                                         where search(x).Contains(SearchBox.Text)
                                         select x;

        loadList(customers);

    }

我在 foreach 开始时的 loadList 方法中收到此错误:

'LINQ to Entities 不支持 LINQ 表达式节点类型'Invoke'。'

我该如何解决这个问题?我对这一切都很陌生,我尝试了几件事,但都没有奏效。

【问题讨论】:

  • _searches = new Func&lt;customer, string&gt;[] 更改为 _searches = new Expression&lt;Func&lt;customer, string&gt;&gt;[] 然后使用 LinqKit:stackoverflow.com/questions/36736203/combining-expression-trees
  • 好的,但在问题中 Linq 是在链中使用的(或任何你称之为的)。我怎样才能像我一样使用 Linq 来做到这一点?

标签: c# entity-framework linq


【解决方案1】:

haim770在评论中给出了答案,但您似乎仍然遇到了一些麻烦,所以我将扩展一下。首先安装LinqKit nuget 包。然后更改您的搜索列表以包含表达式而不是委托:

private static readonly Expression<Func<customer, string>>[] _searches;

然后改变你的搜索方式如下(添加using LinqKit):

IQueryable<customer> customers = from x in context.customers.AsExpandable()
                                 where search.Invoke(x).Contains(SearchBox.Text)
                                 select x;

【讨论】:

  • 好的,谢谢您的回答。我这样做了,但现在出现以下错误:System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String Invoke[customer,String](System.Linq.Expressions.Expression1[System.Func2[Linq.customer,System.String]], Linq.customer)' method, and this method cannot be translated into a store expression.' 知道如何解决这个问题吗?我安装了 Linqkit,并添加了 using LinqKit
  • 别忘了加AsExpandable()
猜你喜欢
  • 2011-08-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多