---
title: "Dynamic table names in Entity Framework LINQ"  
description: "Dynamic table names in Entity Framework LINQ"  
author: "Utpal Vishwas"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159845/dynamic-table-names-in-entity-framework-linq  
category: "linq"  
tags: ["linq", "entity framework", "database table"]  
reading_time: 2 minutes  

---

# Dynamic table names in Entity Framework LINQ

[Dynamic table](https://www.mindstick.com/forum/34298/i-have-created-dynamic-table-in-javascript-and-how-to-save-data-in-sql-db) names in [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach) [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries)

## Replies

### Reply by Aryan Kumar

Yes, [dynamic](https://www.mindstick.com/blog/11080/features-of-java-dynamic-complied-and-interpreted) [table names](https://www.mindstick.com/forum/159128/how-to-get-list-of-table-names-in-different-schema-of-an-oracle-database) can be used in [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) LINQ. You can do this by using the `DynamicTableName` property of the `DbContext` class. The `DynamicTableName` property takes a lambda expression as a parameter. The lambda expression will be used to dynamically generate the table name.

The following code shows how to use the `DynamicTableName` property to dynamically generate the table name:

C#

```plaintext
using System.Linq;

namespace EFCoreDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a DbContext
            using var context = new MyContext();

            // Dynamically generate the table name
            var tableName = context.DynamicTableName(x => x.Id);

            // Get the entities from the table
            var entities = context.Set<Customer>(tableName).ToList();

            // Print the entities
            foreach (var entity in entities)
            {
                Console.WriteLine(entity.Name);
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
```

In this example, we first create a DbContext named `MyContext`. Then, we use the `DynamicTableName` property to dynamically generate the table name. The lambda expression takes the entity ID as a parameter and returns the table name. Finally, we get the entities from the table and print them.

Here are some other things to keep in mind when using dynamic table names with Entity Framework LINQ:

- The lambda expression must be a valid expression that returns a string.
- Lambda expressions must be deterministic. This means that the same input should always produce the same output.
- Lambda expressions must be thread-safe. This means that the expression must be safe to be executed concurrently by multiple threads.


---

Original Source: https://www.mindstick.com/forum/159845/dynamic-table-names-in-entity-framework-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
