0

I'm trying to run a delete statement on EF 5.0. that deletes let say 5000 ~ 40000 records here is my code:

using(myEntity ctx = new myEntity) {
var q = from s in ctx.MyTable.where( x => x.accountID == 1234) select s;
ctx.myTable.Remove(q);     
// because there is no ctx.DeleteObject(whatever) 
// since this is not EF 4.0
}

and here is the error: cannot convert from 'System.Linq.IQueryable' to 'namespace.myTable'

any idea?

Habib
  • 219,104
  • 29
  • 407
  • 436
Sash the man
  • 25
  • 1
  • 5

2 Answers2

1

In your example, you are getting an IQueryable< myTable > and trying to pass it into a Remove method with the following signature:

public TEntity Remove(TEntity entity)

Since it only accepts an instance of an entity, you must actually convert the IQueryable to a list of entities and then iterate through them. I would most likely do it as follows myself:

using(var ctx = new myEntity())
{
    ctx.myTable
       .Where(x => x.accountId == 1234)
       .ToList()
       .ForEach(item => ctx.myTable.Remove(item));

    ctx.SaveChanges();
}
jclake
  • 101
  • 3
0

You are trying to remove a LINQ query from your table (which makes no sense), as opposed to removing a table entry.

What you need to do is either this:

using(myEntity ctx = new myEntity) {
    var q = from s in ctx.MyTable.where( x => x.accountID == 1234) select s;
    foreach(var entry in q)
    {
        ctx.myTable.Remove(entry);     
    }
}

Or write a stored procedure, import it into Entity Framework and execute it.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68