When trying to execute the following query
var del = context.Documents.Remove((context.Documents.Where(doc => doc.DocumentID == ((int)dataGridView1.SelectedCells[0].Value)).FirstOrDefault()));

The error takes off:

A raw exception of type "System.NotSupportedException" in EntityFramework.SqlServer.dll

For more information, the LINQ to Entities doesn’t recognize the system.Windows.Forms.DataGridViewCell get_Item (Int32) method.

Where is the mistake?

    1 answer 1

    Move (int)dataGridView1.SelectedCells[0].Value to variable

    An error occurs because EF cannot convert this expression to a SQL query.

    • Thank. Works. While waiting for a response, it turned out to do this further here var del = context.Documents.Remove(context.Documents.AsEnumerable().Select(doc => doc).Where(doc => doc.DocumentID == ((int)dataGridView1.SelectedCells[0].Value)).FirstOrDefault()); - Pyrejkee
    • @ PyrejkeePureshkin you did something stupid. Read what makes AsEnumerable ... - Pavel Mayorov