Query Variable EntityFramework

0

I want to bring only some fields of what the consultation brings me,

 var result = dbContext.Cliente.Include("Contacto.Cliente").ToList();

this brings me the following:

 "Objects": [
{
  "IdCliente": 3,
  "Identificacion": 10,
  "Nombre": "Ebony Kemp",
  "Direccion": "855-3865 Ipsum Street",
  "Telefono": "1626010138799",
  "Estado": true,
  "FechaCreacion": "2012-06-18T10:34:09",
  "FechaModificacion": "2012-06-18T10:34:09",
  "UsuarioCreacion": "7a4f4875-0c70-fa98-b29b-331ff027fd67",
  "UsuarioModificacion": "44a1e2b1-2be6-fad2-84e1-6eb3cec55cc0",
  "Contacto": [
    {
      "IdContacto": 2,
      "Nombres": "juan",
      "Apellidos": "hincapie",
      "Direccion": null,
      "Telefono": "555555",
      "Email": null,
      "Estado": true,
      "IdCliente": 3,
      "IdTipoContacto": 1,
      "FechaCreacion": null,
      "FechaModificacion": null,
      "UsuarioCreacion": null,
      "UsuarioModificacion": null
    }
  ]
},

But I want you to just return me: Client: Identification and Name Contact: Names but only those that are true and have the IdTypeContact : 1 (The ContactType is related to a ContactType table),

Additionally, I would have to return a list of type Client (not anonymous)

Could you help me in how to bring only those data with the best possible practice? Thanks!

    
asked by Kmiilo Berrio Montoya 02.09.2016 в 22:34
source

3 answers

1

For this you will have to make a more concrete linq

var result = from c in dbContext.Cliente
             select new {
                Identificacion = c.Identificacion,
                Nombre =c.Nombre,
                Contacto = c.Contacto.Where(x=> x.Estado && x.IdTipoContacto == 1)
                                        .Select(x=> x.Nombres )
            };

in this case just define the fields you need and filter the contacts associated with the client. It is assumed that between the client and their contacts there is a relationship that allows the association to navigate.

If you do not want to return an anonymous object, you are going to have to create a class such as:

public class ClienteDto{
   public int Identificacion {get;set;}
   public string Nombre {get;set;}
   public string Contacto {get;set;}
}

then the linq would be

var result = from c in dbContext.Cliente
             select new ClienteDto() {
                Identificacion = c.Identificacion,
                Nombre =c.Nombre,
                Contacto = c.Contacto.Where(x=> x.Estado && x.IdTipoContacto == 1)
                                        .Select(x=> x.Nombres )
            };
    
answered by 05.09.2016 / 13:33
source
0

By making a query with LINQ you can solve the problem:

var elementos= from cliente in dbContext.Cliente
               Join contacto in dbContext.Contacto on contacto.IdCliente equals cliente.IdCliente
               Where contacto.Estado==true && contacto. IdTipoContacto == 1
Seletc new
{
identificacion=cliente.Identificacion
clienteNombre=cliente.Nombre
contactoNombre=contacto.Nombre
};
return elementos.toList();

This returns a list of anonymous objects with the fields -identification, -clientName, -contactsNombre ....

    
answered by 05.09.2016 в 15:12
0

For what you want you could create a class where you will present the data you want: For example:

 class ClientePresenter
    {
public ClientePresenter(){}
    public int Id{get;set;}//esto para tener referencia de que registro es..
    public int Identificacion{get;set;}
    public string clienteNombre{get;set;}
    public string contactoNombre{get;set;}

    }

and then perform:

var result = from c in dbContext.Cliente
             select new ClientePresenter{
                Id=c.Id
                Identificacion = c.Identificacion,
                clienteNombre =c.Nombre,
                contactoNombre = c.Contacto.Where(x=> x.Estado && x.IdTipoContacto == 1)
                                        .Select(x=> x.Nombres )
            };
    
answered by 05.09.2016 в 17:23