Pregunta

I have three tables joined. Store, StoreReport and Report.

Store table
StoreId 
Storename.

Report table
ReportId
ReportName

StoreReport table
StoreId
ReportId

I would like to get all the Stores that haven't filled in a specific report. So far I have this but it counts those who are connected to other reports also.

var reports = from u in db.tblStores
                              join sr in db.tblStoreReports.Where(a => a.ReportId != reportId && !a.Deleted)
                        on u.StoreId equals sr.StoreId into g
                    where !g.Any()
                    select u;
¿Fue útil?

Solución

from u in db.tblStores
where !u.StoreReports.Any(a => a.ReportId == reportId && !a.Deleted)
select u

You can use !.Any to make sure it does not have a Report with certain id

Otros consejos

Isn't it simpler? You can use Where without a join:

var reports = from u in db.tblStores
              where !db.tblStoreReports.Any(sr => sr.StoreId == u.StoreId 
                                               && sr.ReportId == reportId 
                                               && !sr.Deleted)
              select u;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top