Compare shorter term between Asp.net columns

Asked

Viewed 31 times

0

Making order system, I have several warehouses, let’s assume I have to compare 2 warehouses that have shorter delivery time using entityframework,how would I do that? I’m trying to do more it just returns the last value consulted, but not the smallest. in the code below, I have a foreach that runs through 2 warehouses and in the end it would return me only the with shorter delivery time

                foreach (var item2 in lstDeposito)
            {

                Armazem arm = db.Armazem.Find(item2.Deposito.ArmazemId);

                var de = db.Armazem.Where(x => x.Id == arm.Id).Min(x => x.Prazo);

                int menorprazo = Convert.ToInt32(de);
            }

Armazen

public class Armazem
{
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Nome { get; set; }

        public int Prazo { get; set; }

        public virtual ICollection<Deposito> Deposito { get; set; }

}

2 answers

0

As lstDeposito has the warehouses, you can sort and pick up the last record:

var armazen = lstDeposito.OrderByDescending(x => x.Prazo).Take(1);

int menorprazo = armazen.prazo;

0

I did some tests here, and your code returns the lowest value correctly, I did tests with other tables, but the idea is the same:

var valorMenorSalario = context.Funcionarios
                  .Where(x => x.Nome.Equals("Richard"))
                  .Min(x => x.Salario).Value;

In my case, I have more employees with the name Richard but I’m taking the lowest wages between them.

A tip I can give you is to know what is being consulted by the Entity Framework, add this code (if you don’t already have it) on CONSTRUTORof your CONTEXT:

    class PiccoloContext : DbContext
    {
        public DbSet<Funcionario> Funcionarios { get; set; }
        public DbSet<Pagamento> Pagamentos { get; set; }

        public PiccoloContext() : base("Piccolo")
        { 
           Database.Log = sql => System.Diagnostics.Debug.WriteLine(sql);
        }
    }

That line: Database.Log = sql => System.Diagnostics.Debug.WriteLine(sql); Allows you to see what the Entity Framework is doing to query in the Database, just open the tab OutPutor Saída of your Visual Studio that as soon as you go through the query it will show the code done, thus:

SQL Feito pelo Entity Framework Then you take this code and query in the Database you are using, you can copy and paste there that will work. In my case the table was this:

Tabela Funcionarios

And the result was this with the consultation: Resultado da Consulta Feita pelo Entity

Browser other questions tagged

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