'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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
