Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!