Я хочу сократить код, удалив повторяющийся код в лямбда-выражениях C#
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
Что я уже пробовал:
Я пытался передать его в метод как объект и строку, но они терпят неудачу.
private void filterlist(string column, string selection) { AddFilterAndRefresh( "label", Silentmachines => !string.IsNullOrWhiteSpace(column) && column == selection); }
Afzaal Ahmad Zeeshan
Где находится код, который вы хотите минимизировать?
Member 10654482
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); }