0

Convert string reference name to a lambda expression query.

This is my tested code:

protected readonly ApplicationDbContext _context;

public AssetRequestController(ApplicationDbContext context)
{
    _context = context; 
}

string tableName = "FlowStep";

var dbset = QueryExtensions.Query(_context, tableName);

public static partial class QueryExtensions
{
    public static IQueryable Query(this DbContext context, string entityName) =>
            context.Query(context.Model.FindEntityType(entityName).ClrType);

    static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set));

    public static IQueryable Query(this DbContext context, Type entityType) =>
            (IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null);
}

My DbContext

public class ApplicationDbContext : IdentityDbContext
{
   public DbSet<FlowStep> FlowStep { get; set; }
}

I am using System.Linq.Dynamic.Core to dynamically add in lambda expressions to queries in EF.

I want to convert a string name (model name) to lambda expressions query. I found this answer.

Dynamically access table in EF Core 2.0

But it gives an error

System.NullReferenceException: 'Object reference not set to an instance of >an object.'

Microsoft.EntityFrameworkCore.Metadata.IModel.FindEntityType(...) returned >null.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RedAnz
  • 89
  • 2
  • 2
  • 12
  • 1
    Why do you want to use such code in the first place? If you want to base your query on SQL you can use `FromSQL` to pass the original query and [combine it with LINQ](https://learn.microsoft.com/en-us/ef/core/querying/raw-sql) – Panagiotis Kanavos Jul 04 '19 at 08:33
  • Apart from that, *entities* aren't tables. They are domain classes that are mapped to database results that may come from one or more tables or queries. If you know the entity type, why not just use it? LINQ queries can be composed already, in fact every `from ... ` or `.Where()` adds yet another operator to an IQueryable query. – Panagiotis Kanavos Jul 04 '19 at 08:39
  • Finally, [the docs warn that Model isn't meant to be used by client code](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.internal.model?view=efcore-2.1). There may not *be* a `Model` at the time you make that call. If you know you want the `FlowType` property why not request *that* with reflection? – Panagiotis Kanavos Jul 04 '19 at 08:40
  • _context."entityName", I'm parsing this **"entity name"** dynamically. but it will not be recognized as an entity – RedAnz Jul 04 '19 at 11:55
  • *Why*? You don't have to. In any case you are *not* parsing, you're trying to use an internal method that probably *doesn't* do what you expect. You can get the already configured `DbSet FlowStep` property with reflection, eg `_context.GetType().GetProperty("FlowStep")` and read its value with `GetValue`. – Panagiotis Kanavos Jul 04 '19 at 14:56
  • @PanagiotisKanavos thanks for the reply, and it works fine, but I want a complete lambda expression with where clause. can you give an example? – RedAnz Jul 05 '19 at 04:36

0 Answers0