Q:

Linq to sql calculating a percentage of a group of items in query

New to Linq to SQL and novice in SQL.  First post so please be gentle.
I have something similar to the following table I am querying into a dataGridView based on a date range in C#.
HeatNumber ChargeNumber    Weight     DOB
1            1              500     8/26/15
1            2              3500    8/26/15
1            3              2200    8/26/15
2            1              2000    8/27/15
2            2              1100    8/27/15

var query = from SU in dct.GetTable<ScrapInCharge>()
                        where ((SU.DOB >= dateTimePicker2.Value.Date) &&
                               (SU.DOB <= dateTimePicker1.Value.Date))
                        orderby SU.HeatNumber descending
                        select SU;
            scrapInChargeBindingSource.DataSource = query;

I need to add a column that shows the percentage of the total HeatNumber weight that each ChargeNumber makes up.  I did figure how to get the total weight of each heat by HeatNumber.
var TotalHeatWgt = from a in dct.ScrapInCharges
                           where ((a.DOB >= dateTimePicker2.Value.Date) &&
                               (a.DOB <= dateTimePicker1.Value.Date))
                           group a.Weight by a.HeatNumber
                               into b
                               select new { HeatNumber = b.Key, TotalWgt = b.Sum() };

I am currently stuck on how to combine this into a single C# query inserting a % of Heat column after the weight column. Can this be done or would I need to add an unbound column to the dataGridView % of Heat and iterate over the rows using my return values of the TotalHeatWgt query?

A:

Simple attempt:
var results = data.Select(d => new {
    d.HeatNumber,
    d.ChargeNumber,
    d.Weight,
    Percent = 100.0 * d.Weight / data.Where(dd => dd.HeatNumber == d.HeatNumber).Sum(dd => dd.Weight)
});

or
from row in data
select new { 
    row.HeatNumber,
    row.ChargeNumber,
    row.Weight,
    Percent = 100.0 * row.Weight / (from innerRow in data
                                        where innerRow.HeatNumber == row.HeatNumber
                                        select innerRow.Weight
                                    ).Sum()
};

The one below will be more performant (at least in memory, I'm not sure if it'll be better in SQL):
var results = data.GroupBy (d => d.HeatNumber)
    .SelectMany (grp => grp.Select(row => new {
        row.HeatNumber,
        row.ChargeNumber,
        row.Weight,
        Percent = 100.0 * row.Weight / grp.Sum(dd => dd.Weight)
    })
); 

And with query syntax:
from row in data 
group row by row.HeatNumber into grp
from innerRow in grp
select new { 
    innerRow.HeatNumber,
    innerRow.ChargeNumber,
    innerRow.Weight,
    Percent = 100.0 * innerRow.Weight / grp.Sum(dd => dd.Weight)
};

They both print this result:
HeatNumber    ChargeNumber     Weight  Percent 
1             1                500     8.06451612903226 
1             2                3500    56.4516129032258 
1             3                2200    35.4838709677419 
2             1                2000    64.5161290322581 
2             2                1100    35.4838709677419 

Is that what you're after? I've omitted the filtering by date just for simplicity, since the actual problem comes after that

