increment field with lambda

1

I need to insert an increment in a field that is the primary key of a table, but I need to make a select for one more field and it will not work, if I use only with the primary key it 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);
        }
    }
    
asked by anonymous 26.03.2017 / 03:55

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 code above I'm selecting all the records that are null or that is not null that the EMP_ID_80 attribute is equal to the EMP_ID and then I'm getting the maximum.

Why?

In your code you were getting 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

    
26.03.2017 / 04:40