How to make a lambda pivot in a C#list

Asked

Viewed 131 times

3

I’m having a hard time making a pivot command on a list that’s coming from the bank.

inserir a descrição da imagem aqui

Above the select that is coming with the data.. The data field q I would like to put the pivot command.

Follow my class:

public class RelatorioPrincipalModel
{
    public string CodRelatorio { get; set; }
    public string AssuntoRelatorio { get; set; }
    public string DescricaoRelatorio { get; set; }
    public string LojaRelatorio { get; set; }
    public string DataRelatorio01 { get; set; }
    public string DataRelatorio02 { get; set; }
    public string DataRelatorio03 { get; set; }
    public string DataRelatorio04 { get; set; }
    public string DataRelatorio05 { get; set; }
    public string DataRelatorio06 { get; set; }
}

And the image of how it should appear on the page: inserir a descrição da imagem aqui

I am bringing the information in an identical list to the first image and I will add the data in another list with the dates on the same line.

  • Post the final grid generation code... you want to do by c# or sql?

  • @Marllonnasser.. I want to do it in C# lambda. The final code is exactly the class I posted. But follow the code below: List<Reportavailable Reportaryavailableto(18,User logged in. Idusuario, User logged in.Cpf);

  • What you want together in the same columns are the fields public string LojaRelatorio { get; set; }&#xA; public string DataRelatorio01 { get; set; }&#xA; public string DataRelatorio02 { get; set; }&#xA; public string DataRelatorio03 { get; set; }&#xA; public string DataRelatorio04 { get; set; }&#xA; public string DataRelatorio05 { get; set; }&#xA; public string DataRelatorio06 { get; set; }

  • @Marconciliosouza actually I want to align the dates coming from the bank. Ie for each code, Subject, Description and shop I have 5 dates. And I want to put in a single line.

1 answer

0

Try to do the GroupBy and then a Select to join its columns in a single if that is its purpose. .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RelatorioPrincipalModel Disponivel = new RelatorioPrincipalModel()
            {
                CodRelatorio = "Teste",
                AssuntoRelatorio = "Teste",
                DescricaoRelatorio = "Teste",
                LojaRelatorio = "Teste",
                DataRelatorio01 = "DataRelatorio01",
                DataRelatorio02 = "DataRelatorio02",
                DataRelatorio03 = "DataRelatorio03",
                DataRelatorio04 = "DataRelatorio04",
                DataRelatorio05 = "DataRelatorio05",
                DataRelatorio06 = "DataRelatorio06",
            };

            List<RelatorioPrincipalModel> lstDisponivel = new List<RelatorioPrincipalModel>();
            lstDisponivel.Add(Disponivel);

            var lista = lstDisponivel
                .GroupBy(R => new { R.CodRelatorio, R.AssuntoRelatorio, R.DescricaoRelatorio, R.LojaRelatorio })
                .Select(R => new
                {
                    CodRelatorio = R.Key.CodRelatorio,
                    AssuntoRelatorio = R.Key.AssuntoRelatorio,
                    DescricaoRelatorio = R.Key.DescricaoRelatorio,
                    LojaRelatorio = R.Key.LojaRelatorio,
                    Data = R.Select(C => C.DataRelatorio01.ToString() + " "
                    + C.DataRelatorio02.ToString() + " " + C.DataRelatorio03.ToString() + " " + C.DataRelatorio04.ToString() +
                    " " + C.DataRelatorio05.ToString() + " " + C.DataRelatorio06.ToString()).FirstOrDefault(),

                }).ToList();

        }
    }

    public class RelatorioPrincipalModel
    {
        public string CodRelatorio { get; set; }
        public string AssuntoRelatorio { get; set; }
        public string DescricaoRelatorio { get; set; }
        public string LojaRelatorio { get; set; }
        public string DataRelatorio01 { get; set; }
        public string DataRelatorio02 { get; set; }
        public string DataRelatorio03 { get; set; }
        public string DataRelatorio04 { get; set; }
        public string DataRelatorio05 { get; set; }
        public string DataRelatorio06 { get; set; }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.