2

I would like to exlude a query result.

E.g I have:

Table_1 (Id, name)
Table_2 (Id, Table_1_Id)

And, I want to write query which return only the result from Table_1 which does not exist in Table_2. It is a relation of one to many.

Any ideas ?

var query = db.Table_1.Include("Table_2").Where(????
perror
  • 7,071
  • 16
  • 58
  • 85
mskuratowski
  • 4,014
  • 15
  • 58
  • 109

1 Answers1

1

You should use Except:

var query = db.Table_1.Select(MapToType).Except(db.Table_2.Select(MapToSameType));

If you're using an old version of EF that does not support it, you can use All:

var query = db.Table_1.Where(x => db.Table_2.All(y => y.Table_1_Id != x.Id);
Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504