score:1

Accepted answer
public class exampleclass
{
    public int id1 { get; set; }
    public int id2 { get; set; }
    public int usage { get; set; }
    public int usagethis { get; set; }
    public int usagelast { get; set; }
}


        list<exampleclass> listthismonth = new list<exampleclass>
        {
            new exampleclass{id1=1, id2=1,usage=7, usagethis=1, usagelast=0},
            new exampleclass{id1=2, id2=2,usage=4, usagethis=2, usagelast=0},
            new exampleclass{id1=3, id2=3,usage=1, usagethis=3, usagelast=0},
        };

        list<exampleclass> listlastmonth = new list<exampleclass>
        {
            new exampleclass{id1=1, id2=1,usage=3, usagethis=1, usagelast=1},
            new exampleclass{id1=4, id2=4,usage=3, usagethis=4, usagelast=3},
            new exampleclass{id1=2, id2=2,usage=1, usagethis=8, usagelast=6},
        };

        var result = listthismonth.select(a=>new {value=a, list=1})
            .union(listlastmonth.select(a => new { value = a, list = 2 }))
            .groupby(a => new { id1 = a.value.id1, id2 = a.value.id2 })
            .select(x => new exampleclass
            {
                id1 = x.key.id1,
                id2 = x.key.id2,
                usagethis = x.any(o => o.list == 1) ? x.first(o => o.list == 1).value.usage : 0,
                usagelast = x.any(o => o.list == 2) ? x.first(o => o.list == 2).value.usage : 0,
                usage = x.sum(o=>o.value.usage)
            }).tolist();

        //id1   id2 current last    sum
        //1     1   7       3       10
        //2     2   4       1       5
        //3     3   1       0       1
        //4     4   0       3       3

score:0

// left join the current month with the last month
var currentmonth = 
    from current in resourcesthismonth
    join last in resourceslastmonth on new { current.subscriptionid, current.itemid } equals new { last.subscriptionid, last.itemid } into outer
    from o in outer.defaultifempty()
    select new resource
    {
        subscriptionid = current.subscriptionid,
        itemid = current.itemid,
        unitsthismonth = current.units,
        unitslastmonth = o?.units ?? 0, // replace null with 0
        effectiverate = current.effectiverate,
        resourcename = current.resourcename,
        unitofmeasure = current.unitofmeasure
    };

// reverse of the first join.  last month left join current month
var lastmonth = 
    from last in resourceslastmonth
    join current in resourcesthismonth on new { last.subscriptionid, last.itemid } equals new { current.subscriptionid, current.itemid } into outer
    from o in outer.defaultifempty()
    select new resource
    {
        subscriptionid = last.subscriptionid,
        itemid = last.itemid,
        unitsthismonth = o?.units ?? 0, // replace null with 0
        unitslastmonth = last.units,
        effectiverate = o?.effectiverate ?? last.effectiverate,
        resourcename = o?.resourcename ?? last.resourcename,
        unitofmeasure = o?.unitofmeasure ?? last.unitofmeasure
    };

// union them together to get a full join
var resources = currentmonth.union(lastmonth);

Related Query

More Query from same tag