1

I have a linq query and I think I am doing somthing with Lambdas. (don't really know what it's called, I am very new to this)

This is my code:

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

    static Main()
    {
        _searches = new Func<customer, string>[]
        {
            (c) => customer.city,
            (c) => customer.state,
            (c) => customer.country
        };
    }

    protected virtual void SearchBox_TextChanged(object sender, EventArgs e)
    {
        CustomerContext context = new CustomerContext();

        var search = _searches[ComboBox1.SelectedValue];

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

Now my problem is that I get an error at customer.city, customer.state and so on.

The error is:

An object reference is required for the for the non-static field, method or property 'customer.city' Cannot access non-static property 'city' in static context

I have tried making the field static but that breaks the code, when I try to instantiate the field it breaks aswel. Or atleast, when I change customer.city to new customer().city I thought that's called instantiating, but I am not 100% sure.

So, how do I solve this problem?

EDIT:

 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 });
            }
        }
Luuk Wuijster
  • 6,678
  • 8
  • 30
  • 58
  • customer is a *type name*, not an object reference as required to access a member like city. Surely you meant to use c instead, the first argument of the delegate. – Hans Passant Jun 25 '17 at 13:21

1 Answers1

2
Func<customer, string>[] _searches;

means that you declare an array of functions that return a string and receive an instance variable of type customer

but when you write

 (c) => customer.city,

here you are not using the instance passed (c) but you access directly the class customer by its name and this works only for properties, methods or fields that are declared as static

Instead you just need to use the customer instance that has been passed to you at the where search(x) where x is the instance of a customer extracted by your line from x in context.customers and you receive in the Func as the variable named c

 (c) => c.city,
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Okay, thanks. But I now get the following error: `The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.` when I am loading in my listview. You can see the code for that in de question. – Luuk Wuijster Jun 25 '17 at 13:51
  • @Luuk The problem here comes from how Linq works. In short: in order to build efficient queries, Linq converts common functionality (like `.toLowercase()`) to an sql equivalent. However, when you use custom functions like you did, it won't know how to translate. More details: https://stackoverflow.com/q/5284912/314056 – Christian Jun 25 '17 at 14:00
  • https://stackoverflow.com/questions/15417714/linq-custom-function-in-where – Steve Jun 25 '17 at 14:05
  • Okay, I have looked at those questions and tried something but I does not really work and I don't really get it. I am sorry, but can you please help me? – Luuk Wuijster Jun 25 '17 at 14:17
  • I suppose that the best action is to post a new question with the details of the error. It is now something different and I can't help you on that – Steve Jun 25 '17 at 14:27
  • Okay, Will do that. Thanks for your help anyway. – Luuk Wuijster Jun 25 '17 at 14:49