Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda