'Left Join, Where and GroupBY clause using LINQ C#

I am trying to convert the following query into a LINQ statement.

select u.code, u.id, count(d.unitid) from Unit u
Left join Package d on u.id= d.unitid and d.sstatus = 'Open'
where d.hunit is not null
group by u.code, u.id

This is what I have,

var result = 
    from U in Table<Unit>().ToList()
    join D in Table<Package>().Where(x => x.status == "Open").ToList() on U.Id equals D.UnitId into def1
    from def2 in def1.DefaultIfEmpty()
    group def2 by new
    {
        U.Id,
        U.code
    } into grouped
    select new
    {
        Hmy = grouped.Key.Id,
        Code = grouped.Key.Code,
        TotalPAckages = grouped.Count()
    };

But, in above code, I am also getting the results with UnitId is null in Package table. I am not sure where and how to apply the Where clause part (where d.hunit is not null) in the above LINQ statement.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source