Home > Net >  Is it possible to add linq to the end of a method that returns rows from a database? I just want to
Is it possible to add linq to the end of a method that returns rows from a database? I just want to

Time:02-10

private void cboEventFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBoxItem item = cboEventFilter.SelectedItem as ComboBoxItem;
        
        switch (item.Content)
        {
            case "Date":
                //Original method
                datActiveEvents.ItemsSource = _eventManager.RetreieveActiveEvents();
                //Something like this line commented out

                //datActiveEvents.ItemsSource = _eventManager.RetreieveActiveEvents().OrderBy<EventDateID>;
                break;
            case "Time":
                MessageBox.Show("Time filter selected");
                break;
            case "Name":
                MessageBox.Show("Event Name filter selected");
                break;
            case "Sublocation":
                MessageBox.Show("Sublocation filter selected");
                break;
        }

    }

CodePudding user response:

If the return type of RetreieveActiveEvents() is an IEnumerable<T>, you could use the OrderBy LINQ extension method:

datActiveEvents.ItemsSource = _eventManager.RetreieveActiveEvents()
    .OrderBy(x => x.EventDateID);

If the return type is a non-generic IEnumerable, you could use the Cast extension method to convert it to an IEnumerable<T> before you sort the results:

datActiveEvents.ItemsSource = _eventManager.RetreieveActiveEvents()
    .Cast<YourClassWithTheEventDateIDProperty>()    
    .OrderBy(x => x.EventDateID);
  •  Tags:  
  • Related