Member 10654482 Ответов: 1

Я хочу сократить код, удалив повторяющийся код в лямбда-выражениях 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);
            }

1 Ответов

Рейтинг:
9

George Swan

Я не знаю, что ты пытаешься сделать, но ...

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

Будет установить переменную в запрос на IEnumerable<Test_SilentDevices> Где CUSTOMER_NAME равняется "Боб". Вы можете перечислить переменную с помощью a foreach заявление. Кроме того, вы можете вернуть List из запроса.
List<Test_SilentDevices> queryList = silentDevices.Where(tsd => tsd.CUSTOMER_NAME == selection).ToList();

В этом нет необходимости. string.IsNullOrWhiteSpace тест. Если вы хотите выбрать только DEALER_NAME свойство, которое вы можете добавить Select заявление.
List<string> dealerNamesList = silentDevices.Where(tsd => tsd.CUSTOMER_NAME == selection)
                                                  .Select(tsd => tsd.DEALER_NAME).ToList();