简体   繁体   English

如何在 c# linq 中使用跨表查询?

[英]How can i use cross Tab Query in c# linq?

var TatalFeeCollectionDetail = new List<lcclsTotalFeeCollectionDetail>();
TatalFeeCollectionDetail = (from t in .......
                            group t by new { t.MonthName,t.FeeParticularName }                                        
                                into grp                                           
                                select new lcclsTotalFeeCollectionDetail                                           
                                {
                                    Month = grp.Key.MonthName,
                                    Particular = grp.First().FeeParticularLedgerName,
                                    Amount = grp.Sum(t => t.Amount),
                                }).ToList();
dgvTotalFeeCollectionDetail.DataSource = TatalFeeCollectionDetail;

My result is this.............我的结果是这样的............

    Month   particular Amount   
    April       b      1    
    April       c      2    
    April       d      1    
    April       e      2
    April       f      1

I want to convert it like this can any1 help me...........I search for it but can,t understood what to do....我想像这样转换它可以帮助我...........我搜索它但不明白该怎么做......

Month   b   c   d   e   f   Total Amount
April   1   2   1   2   1       7

Thanxxxxxxx in advance提前谢谢xxxxxxx

You can't. 你不能 That would mean creating an anonymous type with a variable number of properties, and it's not possible in C# which is strongly typed. 这将意味着创建具有可变数量属性的匿名类型,并且在强类型的C#中是不可能的。

What you can certainly do is storing the 'particular' values in a Dictionary and get a result like 您当然可以做的是将“特定”值存储在字典中,并得到类似

Month     particular                                  Total
April     {{b, 1}, {c, 2}, {d, 1}, {e, 2}, {f, 1}}    7

Nearly nine years after asking: You Could try a GroupJoin in a GroupBy近九年后问:你可以在 GroupBy 中尝试 GroupJoin

string[] particulars = TatalFeeCollectionDetail.Distinct().ToArray();
var crossTabbedData=TatalFeeCollectionDetail.GroupBy(TFCD => TFCD.Month).Select(TFCD => new {Month=TFCD.Key,RowData=particulars.GroupJoin(TFCD, p => p, t => t.Particular, (p, t) => t.Count() == 0 ? 0 : t.Sum(fn => fn.Amount)).ToList()}).ToList();

Should produce something like what you want, but perhaps without column headers.应该产生你想要的东西,但可能没有列标题。

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

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