This is my first question, I'm doing the best I can to be understand.
I want to insert in table that has many unique properties.
I want to use string instead of property in a Linq expression.
I want to make something like this:
public List<string> AddEmploye(Employe pEmploye)
{
List<string> NonUnique = new List<string>();
List<string> Prop = new List<string>
{
"ID",
"NAS",
"CODE",
};
foreach (var prop in Props)
{
var result = (from t in _context.TEMP02A
where t.prop == pEmploye.prop
select t.name).ToList();
if (result.Count() > 0)
NonUnique.Add(prop);
}
return NonUnique;
}
I tried many solutions proposed on StackOverflow but neither worked for my case.
I tried these solutions: 0, 1, 2, 3, and many others, but none of those solutions works for my case.
I expect the result.count to be 0
or higher, but when I tried this solution, I got this error :
LINQ to Entities does not recognize propertyInfo.GetValue.
Any suggestions?
You could try dynamic linq. It's a bit old, but it allows you to do things like:
Where($"property_name_as_string = @0", some_value)
In your case it would be something like:
.Where(prop + " = @0",employee_prop_value_by_reflection_here)
.Select("Name");
As from your example:
var val = GetPropValue(pEmploye, prop);
_context.TEMP02A.Where(prop + " = @0",val);