简体   繁体   中英

System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.'

This is my code:

    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);

    }

I am getting this error in the loadList method at the start of the foreach:

'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.'

How do I solve this? I am fairly new to all this and I've tried a couple things but none of them worked.

The answer was given in a comment by haim770, but it seems you have some troubles with it still, so I'll expand a bit. First install LinqKit nuget package. Then change your search list to contain expressions instead of delegates:

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

Then change your search method as follows (add using LinqKit ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM