Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
How to pass classProperty to LINQ using Expressions

I have a LINQ query, and I want to pass Person parameters to it. Probably it should be something like this.

Expression<Func<Person, long>> exp1 = person.CarId;
Expression<Func<Person, long>> exp2 = person.PetId;

var result = db.people.Select(x => new {PersonName = x.Name, EntityId = exp1}).ToList()
            

How can I do it?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I've used IQueryble to reuse parts of queries. It was something like the following:

var withPets = Get(GetWithPetId);
                
List<WithEntityId> Get(Func<IQueryable<Person>, IQueryable<WithEntityId>> transformer)
{
    transformer(db.People.Where(...))
        .Where( x => x.EntityId > 100)
        .ToList(); 
}

IQueryable<WithEntityId> GetWithPetId(IQueryable<Person> people)
    => people.Select(x => new WithEntityId(x.Name, x.PetId));

IQueryable<WithEntityId> GetWithCard(IQueryable<Person> people)
    => people.Select(x => new WithEntityId(x.Name, x.CarId));
over 4 years ago · Santiago Trujillo Denunciar

0

Vanilla EF do not allow such queries. I would suggest to use LINQKit. It needs just configuring DbContextOptions:

builder
    .UseSqlServer(connectionString)
    .WithExpressionExpanding(); // enabling LINQKit extension

Then you can use your expressions in the following way:

var result = db.people
   .Select(x => new {PersonName = x.Name, EntityId = exp1.Invoke(x)})
   .ToList()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda