Hi,
Based on your table, the equivalent query for this is the following,
Select h.id As tbHeaderId, h.year As tbHeaderYear, h.Month As tbHeaderMonth, Count(d.idHeader) As Count_of_details
From tbDetail d Right Outer Join tbHeader h On d.idHeader = h.id
Group By h.id, h.year, h.Month
Then if we try to convert this query to Linq, this would be the result,
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
var results = from headers in db.tbHeaders
join details in db.tbDetails on headers.id equals details.idHeader into t1
from t2 in t1.DefaultIfEmpty()
group t2 by new { headers.id, headers.Month, headers.year } into gr
select new
{
tbheaderId = gr.Key.id,
tbHeaderYear = gr.Key.year,
tbHeaderMonth = gr.Key.Month,
Count_of_details = gr.Count(x => x.idHeader != null)
};
dataGridView1.DataSource = results;
}