Edwin Klesman
May 2, 2023

--

Yes, it is possible to pass string variables as column names in the select clause using Entity Framework (EF) and LINQ.You can achieve this by using dynamic LINQ. Dynamic LINQ is an extension method library that allows you to build LINQ queries dynamically at runtime.Here's an example of how you can pass a string variable as a column name in the select clause using dynamic LINQ:

```

string columnName = "FirstName";

var result = dbContext.Users.Select("new(" + columnName + ")");

```

Note that using dynamic LINQ in this way can make your code more prone to SQL injection attacks, so make sure to validate and sanitize user input before using it to build dynamic queries.

--

--

Edwin Klesman
Edwin Klesman

Written by Edwin Klesman

Senior dev @Detacom | cross-platform mobile & web dev | Product Maker | SaaS | from app ideas to implementation | Owner eekayonline.com | Music: edsonkailes.com

Responses (2)