I create an excel file by grouping the data separately in a query created in Excel. How can I combine 2 columns formed on separate rows intoonecolumn?

Viewed 36

I am an intern in software business. I have a table that creates excel. I create this excel data by grouping them according to months and years. I have a problem. I have ACTUALVALUE in a column. Another grouped value is FORECASTVALUE and I don't want them to occur on separate lines in a single line. How can I combine two data in one line?

My Code:

        var yeargroup = actulaModel.Items.GroupBy(x => x.YIL).ToList();
        foreach (var year in yeargroup)
        {
            var monthdata = year.GroupBy(x => x.AY).ToList();
            foreach (var month in monthdata)
            {
                var customGroup = month.GroupBy(x => x.GetType().GetProperty(dashboardScaneriosPieChartRequestDto.GROUPBY).GetValue(x, null)).ToList();
                foreach (var lastData in customGroup)
                {
                    var etlgroup = lastData.GroupBy(x => x.ETL_DATE).OrderByDescending(x => x.Key).ToList();
                    var etlfirstgroup = etlgroup.FirstOrDefault();
                    if (etlfirstgroup != null)
                    {
                        var distinct = etlfirstgroup.GroupBy(x => new { x.YIL, x.AY, x.DTINVENTITEMGROUPID, x.ALTK_ACTUAL_VOLUME, x.CUSTGROUPID, x.DTINVENTITEMSUBGROUPID, x.ETL_DATE }).Select(x => x.First()).ToList();
                        foreach (var x in distinct)
                        {
                            result.Add(new ExcelDataDto()
                            {
                                POINTOFSALE = x.Salesroom,
                                CUSTOMERGROUPNAME = x.CustomerGroupName,
                                DTINVENTITEMGROUPNAME = x.DTINVENTITEMGROUPNAME,
                                DTINVENTITEMSUBGROUPNAME = x.DTINVENTITEMSUBGROUPNAME,
                                ACTUALVALUE = Convert.ToInt32(x.ALTK_ACTUAL_VOLUME),
                                //FORECASTVALUE = 0,
                                YIL = x.YIL,
                                AY = x.AY,
                                ETL_DATE = x.ETL_DATE
                            });
                        }
                    }
                }
            }
        }

        var yeargroupforecast = forecastModel.Items.GroupBy(x => x.YIL).ToList();
        foreach (var year in yeargroupforecast)
        {
            var monthdata = year.GroupBy(x => x.AY).ToList();
            foreach (var month in monthdata)
            {
                var customGroup = month.GroupBy(x => x.GetType().GetProperty(dashboardScaneriosPieChartRequestDto.GROUPBY).GetValue(x, null)).ToList();
                foreach (var lastData in customGroup)
                {
                    var etlgroup = lastData.GroupBy(x => x.ETL_DATE).OrderByDescending(x => x.Key).ToList();
                    var etlfirstgroup = etlgroup.FirstOrDefault();
                    if (etlfirstgroup != null)
                    {
                        var distinct = etlfirstgroup.GroupBy(x => new { x.YIL, x.AY, x.DTINVENTITEMGROUPID, x.ALTK_PREDICTION_VOLUME, x.CUSTGROUPID, x.DTINVENTITEMSUBGROUPID, x.ETL_DATE }).Select(x => x.First()).ToList();
                        foreach (var x in distinct)
                        {
                            result.Add(new ExcelDataDto()
                            {
                                POINTOFSALE = x.Salesroom,
                                CUSTOMERGROUPNAME = x.CustomerGroupName,
                                DTINVENTITEMGROUPNAME = x.DTINVENTITEMGROUPNAME,
                                DTINVENTITEMSUBGROUPNAME = x.DTINVENTITEMSUBGROUPNAME,
                                FORECASTVALUE = Convert.ToInt32(x.ALTK_PREDICTION_VOLUME),
                                //ACTUALVALUE = 0,
                                YIL = x.YIL,
                                AY = x.AY,
                                ETL_DATE = x.ETL_DATE
                            });
                        }
                    }
                }
            }
        }

        return result;

Excel spreadsheet as an example:

Sale  Product    Actualvalue Forecastvalue
Yİ    Product A      50
Yİ    Product B      100
Yİ    Product A                   40
Yİ    Product B                   80

Here, I want it to write both the empty Actualvalue or the empty Forecastvalue values on the same line without writing them. How can I do this?

Afterwards, I did something like this, but I am getting an error, am I thinking logically wrong?

        foreach (var x in result)
        {
            result.Add(new ExcelDataDto()
            {
                POINTOFSALE = x.POINTOFSALE,
                CUSTOMERGROUPNAME = x.CUSTOMERGROUPNAME,
                DTINVENTITEMGROUPNAME = x.DTINVENTITEMGROUPNAME,
                DTINVENTITEMSUBGROUPNAME = x.DTINVENTITEMSUBGROUPNAME,
                FORECASTVALUE = Convert.ToInt32(x.FORECASTVALUE),
                ACTUALVALUE = Convert.ToInt32(x.ACTUALVALUE),
                YIL = x.YIL,
                AY = x.AY,
                ETL_DATE = x.ETL_DATE
            });
        }



        return result;

Can you help me what am I doing wrong?

0 Answers
Related