Я вижу пару проблем с кодом в вашем вопросе.Но причина, по которой удаление строки не отражается в базе данных, заключается в .ToList () - по сути, вы создаете новый список, который является копией запроса и сеткиудаляет элементы из этой копии.
Вы должны использовать ListCollectionView и использовать Filter вместо оператора linq.
Вот пример, показывающийкак это сделать:
1) Создать новый проект WPF с именем ListCollectionViewTest
2) В MainWindow.xaml.cs вырезать и вставить следующее (должно быть в ViewModel, ноМне лень)
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
namespace ListCollectionViewTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<Employee> equivalentOfDatabase = new List<Employee>()
{
new Employee() { FirstName = "John", LastName = "Doe", IsWorthyOfAttention = true },
new Employee() { FirstName = "Jane", LastName = "Doe", IsWorthyOfAttention = true },
new Employee() { FirstName = "Mr.", LastName = "Unsignificant", IsWorthyOfAttention = false },
};
public ListCollectionView TestList { get; set; }
public MainWindow()
{
DataContext = this;
// This is all the magic you need -------
TestList = new ListCollectionView(equivalentOfDatabase);
TestList.Filter = x => (x as Employee).IsWorthyOfAttention;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(equivalentOfDatabase.Aggregate("Employees are: \n\r", (acc, emp) => acc + string.Format(" - {0} {1}\n\r", emp.FirstName, emp.LastName), x => x));
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsWorthyOfAttention { get; set; }
}
}
3) В MainWindow.xaml вырезать и вставить это:
<Window x:Class="ListCollectionViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<DataGrid ItemsSource="{Binding TestList}"
RowHeight="22"
AutoGenerateColumns="True">
</DataGrid>
<Button Content="Show All Employees in DB" Click="Button_Click" />
</StackPanel>
</Window>