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

125
Vistas
C# Pass entity property to a function to use in LINQ

Imagine I have a class like this:

public class BaseRepo<T> where T : class
{
    private readonly DbSet<T> table;

    public BaseRepo(MyDbContext context)
    {
        this.table = context.Set<T>();
    }
}

I want to implement the following logic in to this class.

string GenerateUniqueStringFor(PropertyName)
{
    string val = string.Empty;    

    do
    {
        val = GenerateRandomString();
    }
    while (this.table.FirstOrDefault(x => x.PropertyName == val) is not null);

    return val;
}

The problem is I don't know how to pass Property/PropertyName. The ideal way for calling it would be:

string val = _myRepo.GenerateUniqueStringFor(x => x.PropertyName);
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If I understand correctly, you can try to pass a delegate Func

string GenerateUniqueStringFor(Func<T,string> func)
{
    string val = string.Empty;    

    do
    {
        val = GenerateRandomString();
    }
    while (this.table.FirstOrDefault(x => func(x) == val) is not null);

    return val;
}
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