Entity Framework. Select only 1 from a list in the Include?

0

I'm trying to get just 1 record from the Phones and Emails lists.

var lista = Db.Oportunidades
    .Include(x => x.Conta.Proprietario)
    .Include(x => x.Conta.Contatos.Select(c => c.Telefones))
    .Include(x => x.Conta.Contatos.Select(c => c.Emails))
    .Include(x => x.Estado)
    .Include(x => x.Motivo)
    .Include(x => x.Tipo);

I have tried to use First() , Sigle() , Take(1) that generate the error below.

  

The Include path expression must refer to a navigation property   defined on the type. Use dotted paths for reference navigation   properties and the Select operator for collection navigation   properties. Parameter name: path

Any tips?

    
asked by anonymous 05.12.2017 / 21:49

1 answer

0

It's impossible. The Include function does not support these types of operations.

You can use Select and return an anonymous type containing the master record, telephone number, and email.

Example:

var lista = Db.Oportunidades
    .Include(x => x.Conta.Proprietario)
    .Include(x => x.Estado)
    .Include(x => x.Motivo)
    .Include(x => x.Tipo)
    .Select(x => new 
    {
        Registro = x,
        Telefone = x.Conta.Contatos.Telefones.FirstOrDefault(),
        Email = x.Conta.Contatos.Emails.FirstOrDefault(),
    });
    
06.12.2017 / 11:55