1

I have a db table with columns like DaysInTrade, PercentGain

i want a sum of PercentGain grouped by DaysInTrade

so:

DaysInTrade,PercentGain
1, 5
1,6
2,4

Would give me:

DaysInTrade, Sum
1, 11
2,4

I was not able to get intellisense working in the "select new" part unless I used something like Sum or Select. the DIT column below shows up blank no matter if I do ts.DaysInTrade, or gts.DaysInTrade

        var partialQuery = (from ts in context.TradeSnapshots
                            group ts by ts.DaysInTrade
                            into gts
                            select
                            new
                                {
                                    profit = gts.Sum(s => s.PercentGain),
                                    DIT = gts.Select(ts=>ts.DaysInTrade)
                                });
        dataGridViewTradeSnapshots.DataSource = partialQuery.ToList(); 
4

1 に答える 1

2

グループ化でキーを使用したい。のドキュメントを参照してください。IGrouping<TKey,TElement>

var partialQuery = (from ts in context.TradeSnapshots
                        group ts by ts.DaysInTrade
                        into gts
                        select
                        new
                            {
                                profit = gts.Sum(s => s.PercentGain),
                                DIT = gts.Key
                            });
dataGridViewTradeSnapshots.DataSource = partialQuery.ToList();
于 2012-10-20T16:40:10.340 に答える