0

I have this code below a delete query using linq. But it feels like a long method?. Is there any alternative way to do this?.

var ListOfData = (from a in db.Table1 
                  join b in db.Table2 on a.Table1Id equals b.Table2Id  
                  where a.Table1Id == (param) select a)
                  .ToList();

foreach(var item in ListOfData )
{
    var DelRecord = (from a in db.Table1 
                     join b in db.Table2 on a.Table1Id equals b.Table2Id 
                     where a.Table1Id == item.TableId select a)
                    .FirstOrDefault();

    db.Table1.DeleteObject(DelRecord);
    db.SaveChanges();
  }
Worthy7
  • 1,455
  • 15
  • 28
KiRa
  • 924
  • 3
  • 17
  • 42
  • Why do you not have proper entities created? if they are joinable, you may as well just create a link in the Entity object, so you can reference Table1.table2.whatever. – Worthy7 Apr 10 '17 at 03:05
  • Do you need to save your changes every iteration? – Blue Boy Apr 10 '17 at 03:11
  • Shouldn't it be [ported] on Code Review SE forum? – Failed Scientist Apr 10 '17 at 03:11
  • You could use `RemoveRange` instead of `DeleteObject` – TriV Apr 10 '17 at 03:13
  • Also, what is the point of DelRecord? Can't you just delete 'item' every iteration? – Blue Boy Apr 10 '17 at 03:13
  • Is RemoveRange native EF? – Worthy7 Apr 10 '17 at 03:13
  • Never understood why some people insist on using `join` rather than _navigation properties_ that are already provided in EF. Seems to defeat the purpose of an OO data structure in an ORM environment. _[Don't use LINQ's `join`. Navigate!](https://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/)_ –  Apr 10 '17 at 03:26
  • Sorry all I got slow connection right now. @BlueBoy I can saveChanges after all iteration right?. – KiRa Apr 10 '17 at 03:37
  • @TriV `RemoveRange`?. Its my first time to hear that one.. Maybe I'll study that – KiRa Apr 10 '17 at 03:38

1 Answers1

2

You can remove the second query, you already have the data you need.

var ListOfData = (from a in db.Table1 
                  join b in db.Table2 on a.Table1Id equals b.Table2Id 
                  where a.Table1Id == (param) select a)
                  .ToList();

foreach(var item in ListOfData )
{
    db.Table1.DeleteObject(item);
}
db.SaveChanges();

If you're using Entity Framework 6, they have introduced RemoveRange() method.

var ListOfData = (from a in db.Table1 
                  join b in db.Table2 on a.Table1Id equals b.Table2Id 
                  where a.Table1Id == (param) select a)
                  .ToList();

db.Table1.RemoveRange(ListOfData);
db.SaveChanges();
Worthy7
  • 1,455
  • 15
  • 28
  • Its still the same to mine right?.. And uses a long code – KiRa Apr 10 '17 at 03:04
  • 2
    Good spot about EF6. Apparently _"[Note that if AutoDetectChangesEnabled is set to true (which is the default), then DetectChanges will be called once before delete any entities and will not be called again. This means that in some situations **RemoveRange may perform significantly better than calling Remove** multiple times would do](https://msdn.microsoft.com/en-us/library/system.data.entity.dbset.removerange(v=vs.113).aspx)"_. Nice! +1 –  Apr 10 '17 at 03:31
  • I don't have the `RemoveRange`. How to check what EF that I am using?. – KiRa Apr 10 '17 at 03:50
  • Should be written in your App.config or Web.config, or check your packages list, or open NuGet and seach for EntityFramework – Worthy7 Apr 10 '17 at 03:52
  • Its okay now thanks to [this](http://stackoverflow.com/questions/3377821/determine-version-of-entity-framework-i-am-using) by ChrisS – KiRa Apr 10 '17 at 04:00