2009-09-10 11 views
11

Come posso scrivere una query linq su entità che include una clausola having?Entity Framework T-Sql "having" Equivalent

Ad esempio:

SELECT State.Name, Count(*) FROM State 
INNER JOIN StateOwner ON State.StateID = StateOwner.StateID 
GROUP BY State.StateID 
HAVING Count(*) > 1 

risposta

22

Qual è il motivo per non utilizzare solo una clausola where sul risultato?

var query = from state in states 
      join stateowner in stateowners 
       on state.stateid equals stateowner.stateid 
      group state.Name by state.stateid into grouped 
      where grouped.Count() > 1 
      select new { Name = grouped.Key, grouped.Count() }; 
+0

Dannazione Jon .... haha ​​... hai vinto di nuovo! <3 – womp

+5

raggruppati.Qualsiasi() sarebbe utilizzato per Conteggio()> 0, non Conteggio()> 1 – Lucas

+2

@Craig: Se fosse stato 'Conteggio()> 0', non ci sarebbe stato bisogno di restrizioni a tutto, dato che era un join interno ... –

3

credo che si può utilizzare un GroupBy seguita da una clausola Where e sarà tradurlo come Having. Non del tutto sicuro però.

+2

Per MySql causerà SELECT interno con esterno WHERE, non HAVING. Quindi causerà problemi di prestazioni. – Yuri