简体   繁体   English

可以在IQueryable上使用linq将两列加在一起并按该列分组吗?

[英]Possible to add two columns together and group by that column using linq on IQueryable?

My data comes from IQueryable that looks like the following table when I return all of the DailyCollectionActivities. 我的数据来自IQueryable,当我返回所有DailyCollectionActivities时,其外观如下表所示。

CatType | CdDescr | CollTypeDescr | RollCasteDescr | TIFDescr | TADescr | Amount
--------------------------------------------------------------------------------
Cat 1   | Cd 1    | CollType 233  | Roll Caste 234 | TIF 2344 | TA 2343 | 344.35
Cat 1   | Cd 1    | CollType 222  | Roll Caste 235 | TIF 2345 | TA 2344 | 355.35
Cat 2   | Cd 2    | CollType 223  | Roll Caste 236 | TIF 2346 | TA 2345 | 664.44
Cat 3   | Cd 3    | CollType 255  | Roll Caste 236 | TIF 2347 | TA 2346 | 455.34
Cat 4   | Cd 4    | CollType 266  | Roll Caste 236 | TIF 2348 | TA 2347 | 455.44

I'm trying to find out if it's possible, using linq on the IQueryable<DailyCollectionActivity> data, to add two columns together and then group on that newly created column? 我正在尝试查找是否有可能,对IQueryable<DailyCollectionActivity>数据使用linq,将两个列加在一起,然后对这个新创建的列进行分组? For example I need to figure out how to add CatType to CdDescr (CatType + '-' + CdDescr) and then group by that newly created column and sum the Amounts. 例如,我需要弄清楚如何将CatType添加到CdDescr (CatType + '-' + CdDescr) ,然后按该新创建的列分组并求和。 Finally, I then need to take the results of that query and bind it to a RadGrid . 最后,然后需要获取该查询的结果并将其绑定到RadGrid

To make things more interesting, the user is allowed to choose which columns get added together. 为了使事情更有趣,允许用户选择将哪些列添加在一起。 I could wind up with a group by clause like (CatType + '-' CdDescr), (TIFDescr + '-' + TADescr) . 我可以使用诸如(CatType + '-' CdDescr), (TIFDescr + '-' + TADescr)的group by子句(CatType + '-' CdDescr), (TIFDescr + '-' + TADescr)

Is this something that I can reasonably accomplish using Linq? 这是我可以使用Linq合理完成的事情吗?

say its from categories 从类别中说出

var result = from s in (

from p in categories
             select new {
TypeDesr = p.CatType+"-"+ p.CdDesr,
CollTypeDescr  = p.CollTypeDescr ,
RollCasteDescr = p.RollCasteDescr,
TIFDescr = p.TIFDescr,
TADescr = p.TADescr,
Amount = p.Amount
})
group s by s.TypeDesr into r
select r;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM