How to make a max + 1 with lambda or Linq, but with tables from nothing, all null

Asked

Viewed 83 times

1

I made this method and other attempts. With the table filled, ok, but with the table without data, it is giving error:

public int GeraIdBalanca()
{
    int chave = 0;
    var obj = contexto.Balancas.Max().IdBalanca;
    if (obj == 0)
        chave = 1;
    else
        chave = obj + 1;
    return chave;
}

I tried with the table with information, I can bring the ID, but with the table reset, newly created gives me error.

  • I can not say because I think that it lacks context (without wanting to make pun with the variable), but this code can be written like this: contexto.Balancas.Max().IdBalanca + 1 and gives the same result.

  • if there is no record in the bank, Max() would return null ?

3 answers

3


The whole code can be written like this

public int GeraIdBalanca() => contexto.Balancas.Max()?.IdBalanca + 1 ?? 1;

Your problem is because it is necessary to check whether the return of Max is not null before trying to access the property IdBalanca.

var obj = contexto.Balancas.Max()?.IdBalanca ?? 1;

If you’re using the Entity Framework, I can tell you that your approach doesn’t work and doesn’t make sense.

In RU, the method would need to be like this

public int GeraIdBalanca() => contexto.Balancas.Any() ? contexto.Balancas.Max(b => b.id) + 1 : 1;

Or, in a normal method

public int GeraIdBalanca()
{
    return contexto.Balancas.Any() ? contexto.Balancas.Max(b => b.id) + 1 : 1;
}
  • That for the right C# 7 :)

  • 2

    No, @Marconciliosouza. C# 6.

  • Truth, confuse the version.

  • @LINQ, I did not understand its implementation. The way it is gives the same error, but I did not understand if it is a method or what, the first line of your answer.

  • It is a method. If you are using EF, see the end of the answer.

  • 1

    This edition you solved. I have not tested the second stage of Marconcilio, because the first I had done and gives error.

Show 1 more comment

2

I believe it can be done like this:

        var obj = contexto.Balancas.Max(b => b.IdBalanca);
        int chave = (obj == null ? 1 : obj + 1);
        return chave;
  • Rovann, make that mistake the same as it was: The specified method 'Domain.Entities.Balanca Max[Balanca](System.Linq.IQueryable1[Domain.Entities.Balanca])' on the type 'System.Linq.Queryable' cannot be Translated into a LINQ to Entities store Expression because no Overload Matches the passed Arguments.`

  • I think the method parameter is missing, so I looked here should be something like this: contexto.Balancas.Max(b => b.IdBalanca);

  • I’ll try it like this, but I had done it and it was wrong, but I did it without the ternary

  • Rovann, I had done similar(without the ternary) and it didn’t work. Now in line obj.IdBalancagives me design error, saying int não contém uma definição para IdBalanca e não há método de extensão........

  • the Max in this case is returning an int, do not know if it can be null but just take the obj.IdBalanca and leave only the obj

0

You can use a Default solution for when your table has not had data.

public int GeraIdBalanca()
{
    return contexto.Balancas.Select(m => m.IdBalanca).DefaultIfEmpty(0).Max() + 1;
}

Browser other questions tagged

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