Error while trying to popular a List

Asked

Viewed 281 times

0

I’m trying to create a table with Union, I made a class to popular on ViewModel, in this way:

  public class ListVencimentos {
    public int Id { get; set; }
    public DateTime Data { get; set; }
    public int? TipoDocumentoId { get; set; }
    public Models.TipoDocumento TipoDocumento { get; set; }
    public float Valor { get; set; }
    public float JaPago { get; set; }
    public float Saldo { get; set; }
    public float Desconto { get; set; } 
    public float Mora { get; set; } 
    public float Multa { get; set; }
    public float Outros { get; set; }
    public float Total { get; set; }
    public DateTime? DataPagamento { get; set; }
    public int? DiasAtraso { get; set; }
}

Calling it that public IEnumerable<ListVencimentos> ListVencimentos { get; set; }

And to make the selects got like this, that’s pulling properly:

 var un = db.ContasPagar.Include(c => c.ContasPagarP).Where(c => c.FornecedorId == id).Select(c => new
            {
                Id = c.Id,
                Data = c.Data,
                TipoDocumentoId = c.TipoDocumentoId,
                Valor = c.Valor,
                JaPago = c.JaPago,
                Saldo = c.Saldo,
                Desconto = c.Desconto,
                Mora = c.Mora,
                Multa = c.Multa,
                Outros = c.Outros,
                Total = c.Total,
                DataPagamento = c.DataPagamento,
                DiasAtraso = c.DiasAtraso

            }).ToList()
            .Union(db.FaturaContasPagar.Include(r => r.FaturaContasPagarP).Where(r => r.FornecedorId == id).Select(r => new
            {
                Id = r.Id,
                Data = r.Data,
                TipoDocumentoId = r.TipoDocumentoId,
                Valor = r.Valor,
                JaPago = r.JaPago,
                Saldo = r.Saldo,
                Desconto = r.Desconto,
                Mora = r.Mora,
                Multa = r.Multa,
                Outros = r.Outros,
                Total = r.Total,
                DataPagamento = r.DataPagamento,
                DiasAtraso = r.DiasAtraso
            }).ToList());

Only when I go popular the ListVencimentos, he is returning me the following error.

It is not possible to implicitly convert "System.Collections.Generic.Ienumerable<>" to "System.Collections.Generic.Ienumerable". There is an explicit conversion (there is an absent conversion?)

How can I do, am I forgetting some detail ?

EDIT

I’m trying to fill in this way:

ListVencimentos = un.Select(r => new ListVencimentos { r.Id, r.Data, r.TipoDocumentoId, r.Valor, r.JaPago, r.Saldo, r.Desconto,
                    r.Mora, r.Multa, r.Outros, r.Total, r.DataPagamento })

And he’s returning the error:

It is not possible to initialize type "Stripes" with a collection initializer because it does not implement "System.Collections.Ienumerable"

How can I load the result un, in a table ? Every way I try it returns some kind of conversion error.

  • Change from anonymous type to your type in your .Select(s => new ListVencimentos { ... })

  • He returned to me the following mistake. Unable to initialize type "Stripes" with a collection initializer because it does not implement "System.Collections.Ienumerable" I switched to ListVencimentos = un.Select(r => new ListVencimentos { r.Id, r.Data, r.TipoDocumentoId, r.Valor, r.JaPago, r.Saldo, r.Desconto,&#xA; r.Mora, r.Multa, r.Outros, r.Total, r.DataPagamento })

  • 2

    Are you trying to initialize your class as a list? Example: ListVencimentos lista = un.Select(s => new ListVencimentos {...}); If so, it won’t work because your class is not a collection.

  • @Pedropaulo yes, na ViewModel I declared so public IEnumerable<ListVencimentos> ListVencimentos { get; set; } and in the load of editing is like this: ListVencimentos= un.Select(r => new ListVencimentos { r.Id, r.Data, r.TipoDocumentoId, r.Valor, r.JaPago, r.Saldo, r.Desconto,&#xA; r.Mora, r.Multa, r.Outros, r.Total, r.DataPagamento }),

  • So in my head without analyzing the code as a whole, it gets a little complicated, I’d have to do a better analysis, because apparently that’s what it would be... Will end up generating Flood in comments, if you want to share the discussion calls in chat.

1 answer

1


Create two separate variables to analyze better while debugging your code.

In the first variable we will store the query with the accounts to pay. No Select we will create an instance of your class ListVencimentos, making the query fields associations with the class.

var contas = db.ContasPagar.Include(c => c.ContasPagarP).Where(c=> c.ContasPagarP.FornecedorId == id).Select(c => new ListVencimentos
{
    Id = c.Id,
    Data = c.Data,
    TipoDocumentoId = c.TipoDocumentoId,
    Valor = c.Valor,
    JaPago = c.JaPago,
    Saldo = c.Saldo,
    Desconto = c.Desconto,
    Mora = c.Mora,
    Multa = c.Multa,
    Outros = c.Outros,
    Total = c.Total,
    DataPagamento = c.DataPagamento,
    DiasAtraso = c.DiasAtraso
}).ToList();

In the second variable we will store the query with the invoices to pay. No Select we will create an instance of your class ListVencimentos, making the query fields associations with the class.

var faturas = db.FaturaContasPagar.Include(c => c.FaturaContasPagarP).Where(c=> c.FaturaContasPagarP.FornecedorId == id).Select(c => new ListVencimentos
{
    Id = c.Id,
    Data = c.Data,
    TipoDocumentoId = c.TipoDocumentoId,
    Valor = c.Valor,
    JaPago = c.JaPago,
    Saldo = c.Saldo,
    Desconto = c.Desconto,
    Mora = c.Mora,
    Multa = c.Multa,
    Outros = c.Outros,
    Total = c.Total,
    DataPagamento = c.DataPagamento,
    DiasAtraso = c.DiasAtraso,
}).ToList();

Now that we have all the values we need, just use the method Union with the result of the two previous querys:

var union = contas.Union(faturas).ToList();

I hope I’ve helped!

Browser other questions tagged

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