Imagina que tengo una clase como esta:
public class BaseRepo<T> where T : class { private readonly DbSet<T> table; public BaseRepo(MyDbContext context) { this.table = context.Set<T>(); } } Quiero implementar la siguiente lógica en esta clase.
string GenerateUniqueStringFor(PropertyName) { string val = string.Empty; do { val = GenerateRandomString(); } while (this.table.FirstOrDefault(x => x.PropertyName == val) is not null); return val; }El problema es que no sé cómo pasar Property/PropertyName. La forma ideal de llamarlo sería:
string val = _myRepo.GenerateUniqueStringFor(x => x.PropertyName);Si entiendo correctamente, puede intentar pasar un delegado 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; }