c# - EFCore build a where clause as a string - Stack Overflow

admin2025-04-15  3

In Entity framework I could build a where clause as a string. For example;

var whereClause = "ID > 0 and !Inactive";

pList.AddRange(MyData.Set<T>().Where(whereClause).ReduceCasts().ToList());

I have updated to EFCore and the same statement now gives me the error;

Cannot convert from 'string' to 'System.Func<T, bool>'

Has EFCore not implemented this functionality or have I missed an addon that allows strings to be used in a where statement? I also have the same issue with the .OrderBy(), I assume the answer will apply here as well.

In Entity framework I could build a where clause as a string. For example;

var whereClause = "ID > 0 and !Inactive";

pList.AddRange(MyData.Set<T>().Where(whereClause).ReduceCasts().ToList());

I have updated to EFCore and the same statement now gives me the error;

Cannot convert from 'string' to 'System.Func<T, bool>'

Has EFCore not implemented this functionality or have I missed an addon that allows strings to be used in a where statement? I also have the same issue with the .OrderBy(), I assume the answer will apply here as well.

Share Improve this question asked Feb 4 at 11:12 LanceLance 3635 silver badges13 bronze badges 6
  • 2 You could not do that in EF6 out of the box. You used some extension. Which one was it? – Gert Arnold Commented Feb 4 at 12:15
  • 1 That said, it would be interesting to know why you used this string-based where clause. Maybe you can use EF-core native features to replace it. We might be able to tell if we know a few more details. – Gert Arnold Commented Feb 4 at 12:19
  • 3 Did you previously use the Dynamic Linq package? – DavidG Commented Feb 4 at 12:25
  • Hi, Thanks. I was using Dynamic Linq! I updated to Dynamic.Linq.Core. dynamic-linq.net – Lance Commented Feb 4 at 16:59
  • @GertArnold. I have a load of Attributes and Interfaces on my Entities that my various OnLoadEntityList<T> functions read and build the query's by default (IHaveDisplayOrder, IHaveCodeOrder, IHaveActive etc. as well as SoftDelete and record level security filtering ). Its a pretty big Framework/Application which I'm attempting to update in a short time frame. I will look at programming the functional aspect of these things when the rush is over. Most of it has ported across with not too much hassle – Lance Commented Feb 4 at 17:15
 |  Show 1 more comment

1 Answer 1

Reset to default 0

I was using the original Dynamic Linq package, I upgraded to the latest https://www.nuget.org/packages/System.Linq.Dynamic.Core

转载请注明原文地址:http://www.anycun.com/QandA/1744724171a86742.html