altandogan
altandogan

Reputation: 1285

Selecting the value of a column not in GroupBy

I need a query that selects a column value that the data is not grouped by. I must group by Vkn number to get the Sum of InvoiceCount, but I want to select FirmName without grouping by it.

public Model1
{
    public int VKN {get; set;}
    public string FirmName {get; set;}
    public string {get; set;}
    public string InvoiceCount {get; set;}
}

List<Model1> temp = new List<Model1> { /*adding some value... */ };

var temp = from m in temp group by new { row.VKN, row.FirmName} into g
           select new { FirmName = g.Key.FirName,
                        VKN = g.Key.Vkn,
                        InvoiceCountSum = g.Sum(x => x.InvoiceCount),
                      }

Upvotes: 0

Views: 62

Answers (1)

Vladimirs
Vladimirs

Reputation: 8599

You can group by row.VKN and use almost any aggregate function to get other fields that not participated in grouping, but only if you care which result from grouped collection like that (e.g. you definitely know FirmName is unique per VKN):

FirmName = g.Min(x => x.FirmName)

Upvotes: 1

Related Questions