2009-08-26 16 views
6

Quiero crear esta consulta:tener y recuento condicional() en la consulta LINQ

select Something, count(Something) as "Num_Of_Times" 
from tbl_results 
group by Something 
having count(Something)>5 

empecé con esto:

tempResults.GroupBy(dataRow => dataRow.Field<string>("Something")) 
    .Count() //(.......what comes here , to make Count()>5?) 

Respuesta

8
from item in tbl_results 
group item by item.Something into groupedItems 
let count = groupedItems.Count() 
where count > 5 
select new { Something = groupedItems.Key, Num_Of_Times = count }; 

ACTUALIZACIÓN: Esto le dará el resultado como IQueryable<DataRow>:

DataTable dt= new DataTable(); 
dt.Columns.Add("Something", typeof(int)); 
dt.Columns.Add("Num_Of_Times", typeof(int)); 

var results = (from item in tbl_results 
       group item by item.Something into groupedItems 
       let count = groupedItems.Count() 
       where count > 2 
       select dt.Rows.Add(groupedItems.Key, count)).AsQueryable(); 

(tenga en cuenta que también se llena la tabla dt)

+0

Muchas gracias, necesito el resultado como IQueryable , ¿Hay una manera de crear el resultado 'seleccionar' como IQueryable ? o ¿necesito crear las filas manualmente? – Rodniko

+0

ver respuesta actualizada –

+0

Muchas gracias :) – Rodniko