1

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?

JuanR
  • 7,405
  • 1
  • 19
  • 30
  • 1
    Hi Michael, where does Props come from? Also, I am not sure what your actual question is, could you try and explain it clearer what it is that you are trying to do? – sander Jul 12 '19 at 14:05
  • 1
    It kind of looks like he is trying to access a column by string name using EF, but I am not sure. – JuanR Jul 12 '19 at 14:08
  • Props are my entity properties that are unique column in my database table. I want to use strings instead of entity properties. The goal is to reduce the amount of code line since im reapeating the same process for 7 entity properties. – Michael Desrosiers Jul 12 '19 at 14:11
  • What is the type of `t` in the `_context.TEMP02A` table? Is it also `Employee`? – Chris Dunaway Jul 12 '19 at 14:44
  • Yes! Both type `t` and `pEmploye` are the same – Michael Desrosiers Jul 12 '19 at 14:51

1 Answers1

0

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);
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • I tried this : `var result = _context.TEMP02A.Where($"property_name_as_string == some_value");` But i got this error : cannot convert from 'string' to 'System.Linq.Expression.Expression>' – Michael Desrosiers Jul 12 '19 at 14:19
  • Starting from what you proposed, i went on this : `var result = _context.TEMP02A.Where(t => GetPropValue(t,prop) == GetPropValue(pEmploye, prop)).Select(t => t.name);` . This execute but when i add a `.ToList()`, i go on this error : `System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.Object GetPropValue(System.Object, System.String)' method, and this method cannot be translated into a store expression.'` – Michael Desrosiers Jul 12 '19 at 14:35
  • It looks like the `GetValue()` method does not work in my expression :( – Michael Desrosiers Jul 12 '19 at 14:48
  • From you last exemple, it says : `No overload for method 'Where' takes 2 arguments` – Michael Desrosiers Jul 12 '19 at 14:59
  • Did you include the dynamic linq library? you need to include `using System.Linq.Dynamic` – Stefan Jul 12 '19 at 15:01
  • Im probably missing something else because its said 'Dynamic' does not exist in th namespace 'System.Linq'. I have System.Linq and System.Linq.Expression – Michael Desrosiers Jul 12 '19 at 15:06
  • Install this package: https://www.nuget.org/packages/System.Linq.Dynamic – Stefan Jul 12 '19 at 15:38