increment field with lambda

Asked

Viewed 56 times

1

I need to insert an increment into a field that is the primary key of a table, but need to be selected by one more field and does not work, if use only with the primary key works. But I need to pass the other parameter too.

    public string INSERT_EMBALAGEM(int EMP_ID)
    {
        try
        {
            using (var bancodados = new takeeatEntities2())
            {
                //essa linha funciona 
                long maxes = bancodados.embalagens_80.DefaultIfEmpty().Max(x => x == null ? 1 : x.EML_ID_80 + 1);

                //Essa outra linha é a que eu preciso e não funciona, ela contem um parametro para ser o select da tabela
                long maxes = bancodados.embalagens_80.DefaultIfEmpty().Max(x => x == null ? 1 : x.EML_ID_80 + 1 && x.EMP_ID_80 == EMP_ID);

                embalagens_80 emb = new embalagens_80();
                emb.EML_ID_80 = maxes;
                emb.EMP_ID_80 = EMP_ID;

                bancodados.embalagens_80.Add(emb);
                bancodados.SaveChanges();

                return maxes.ToString();
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

1 answer

0


Solution:

 long maxes = bancodados.embalagens_80.DefaultIfEmpty()
                                      .Where(x=>x==null||x.EMP_ID_80 == EMP_ID)
                                      .Max(x => x == null ? 1 : x.EML_ID_80 + 1) ;

In the above code I am selecting all records that are null or that if it is not null that the attribute EMP_ID_80 is equal to EMP_ID and then I am getting the maximum.

Why?

In your code you were taking a number and trying to filter, technically you were doing this:

.Max(x=>1 && x.EMP_ID_80 == EMP_ID)

which is equal to

if( 1 && EMP_ID_80 == EMP_ID)

What causes a syntactic error

  • thanks buddy worked out. thank you very much

Browser other questions tagged

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