How can i Join, GroupBy and Select with LINQ?

Viewed 56

I am trying to perform Join, GroupBy two columns and select to a new list as

            var v = master.Join(
                detail,
                o => o.Id,
                d => d.OrderId,
                (o, d) => new 
                {
                    DateAdded = o.DateAdded,
                    VatRate = d.VatRatePercentage,
                    InvoiceDescription = o.InvoiceDescription
                }).GroupBy(c => new
                {
                    c.DateAdded,
                    c.VatRate
                })
                .Select(gcs => new 
                {
                    DateAdded = gcs.Key.DateAdded.Date,
                    VatRate = gcs.Key.VatRate,
                    InvoiceTypeDescription = gcs.Key.InvoiceDescription,
                }
                ).ToList();

but i get the error

'<anonymous type: DateTime DateAdded, decimal VatRate>' does not contain a definition for 'InvoiceTypeDescription' and no accessible extension method 'InvoiceTypeDescription' accepting a first argument of type '<anonymous type: DateTime DateAdded, decimal VatRate>' could be found

I would like to group by two columns (DateAdded, VatRate) but get the InvoiceDescription column also in the results. How can i fix that?

1 Answers

This is a sample:

var resultmutiplekeylambaorderbyelement = studentlist  
    .GroupBy(stu => new{stu.standard, stu.age})  
    .OrderBy(g => g.Key.standard).ThenBy(y=> y.Key.age)  
    .Select(g => new { standardkey = g.Key.standard, agekey = g.Key.age, studentobj = g.OrderBy(y => y.name) });
Related