Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a load of similar code repeating in my application and I would like to learn how to tidy it up. I know how to do this for String, Labels & Combo boxes but struggling with this section.
I have an observable collection and I am filtering this base on column values.  The column names (CUSTOMER_NAME) and values (selection) are the only variable sections of the code.  I know how to deal with the value because this is just a string but I do not know how to deal with "Silentmachines.CUSTOMER_NAME" &  "Silentmachines.DEALER_NAME".
Example one
Silentmachines => !string.IsNullOrWhiteSpace(Silentmachines.CUSTOMER_NAME) && Silentmachines.CUSTOMER_NAME == selection)
Example two
Silentmachines => !string.IsNullOrWhiteSpace(Silentmachines.DEALER_NAME) && Silentmachines.DEALER_NAME == selection
Possible code required
public partial class Test_SilentDevices
    {
public string DEALER_NAME { get; set; }
public string CUSTOMER_NAME { get; set; }
}
  public ObservableCollection<Test_SilentDevices> Silentmachines { get; set; }

Is it possible or do I need to leave the current code in place?

Thanks

Phil


What I have tried:

I have tried pass it into a method as a object and string but they fail.

private void filterlist(string column, string selection)
    {
        AddFilterAndRefresh(
                            "label", Silentmachines => !string.IsNullOrWhiteSpace(column) && column == selection);

    }
Posted
Updated 24-Aug-19 9:59am
v3
Comments
Afzaal Ahmad Zeeshan 24-Aug-19 14:12pm    
Where is the code that you want to minimize?
Member 10654482 24-Aug-19 14:32pm    
            if (label == "Customer Name")
            {
                AddFilterAndRefresh(
                             label, Silentmachines => !string.IsNullOrWhiteSpace(Silentmachines.CUSTOMER_NAME) && Silentmachines.CUSTOMER_NAME == selection);
            }
            if (label == "Dealer Name")
            {
                AddFilterAndRefresh(
                               label, Silentmachines => !string.IsNullOrWhiteSpace(Silentmachines.DEALER_NAME) && Silentmachines.DEALER_NAME == selection);
            }
            if (label == "Postal code")
            {
                AddFilterAndRefresh(
                               label, Silentmachines => !string.IsNullOrWhiteSpace(Silentmachines.POSTAL_CODE) && Silentmachines.POSTAL_CODE == selection);
            }

1 solution

I'm not sure what you are trying to do but

string selection = "Bob";
IEnumerable<Test_SilentDevices> results = silentDevices.Where(tsd => tsd.CUSTOMER_NAME == selection);

Will set the query variable to an IEnumerable<Test_SilentDevices> where the CUSTOMER_NAME equals 'Bob'. You can enumerate the variable using a foreach statement. Alternatively you can return a List from the query.
List<Test_SilentDevices> queryList = silentDevices.Where(tsd => tsd.CUSTOMER_NAME == selection).ToList();

There is no need for the string.IsNullOrWhiteSpace test. If you want to select just the DEALER_NAME property you can add a Select statement.
List<string> dealerNamesList = silentDevices.Where(tsd => tsd.CUSTOMER_NAME == selection)
                                                  .Select(tsd => tsd.DEALER_NAME).ToList();

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900