Linq using variable as column name in select new statement

Nezir

I have a LINQ query and as you can see I am trying to use variables as column names.

string first = "someColumnName1";
string sec = "someColumnName2";
string third = "someColumnName3";

var data = (from i in ctx.PlanskaKalkulacijas
            where i.PKid.Value == id
            select new { first = i.NP1, sec = i.NP2, third = i.NP3}
           ).OrderByDescending(m => m.Id)
            .ToList();

this code produce column names in gridview as first, sec and third but I need someColumnName1, someColumnName2, someColumnName3.

Any help ?

Ondrej Janacek

Either rename columns in the gridview after you insert data or name properties of the anonymous type you create properly.

var data = (from i in ctx.PlanskaKalkulacijas 
            where i.PKid.Value == id 
            select new 
            { 
                someColumnName1 = i.NP1, 
                someColumnName2 = i.NP2, 
                someColumnName3 = i.NP3
            }).OrderByDescending(m => m.Id)
              .ToList();

If you don't know values at compile time, just rename it. To do that, follow this question here on SO.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linq using variable as column name in select new statement

From Dev

Finding column name and using it in select statement

From Dev

Simplify IF-clause and find name of Variable in LINQ select statement

From Dev

using a IF type statement in a linq to entities SELECT NEW clause C#

From Dev

Using a function inside of a select statement to make a new column

From Dev

Dynamically select column name using Linq to Entities (EF6)

From Dev

Invalid Column Name in Select Statement

From Dev

Invalid Column Name in Select Statement

From Dev

Variable table name in select statement

From Dev

How to select column name in select statement

From Dev

SQL column name same as PL/SQL variable name - How can this be done in a select statement?

From Dev

SQL column name same as PL/SQL variable name - How can this be done in a select statement?

From Dev

LINQ Dynamically Select Column From Column Name

From Dev

Change output property name in linq select statement

From Dev

Subset a data.frame using a variable for the column name in the select expression

From Dev

Using a variable as the column name

From Java

Using Select and Where in a List using LINQ statement

From Dev

Dynamic ORACLE SQL - How to convert a varchar2 variable to use as a column name in a select statement?

From Dev

Passing a column name in a SELECT statement in Python

From Dev

Change column name in Oracle in SELECT statement

From Dev

Ambiguous column name in rawQuery select statement

From Dev

How to add new column in select statement

From Dev

Table Name Variable in select statement in sql server

From Dev

C# How do I use a variable in a Linq select statement using Query Syntax

From Dev

Declare variable within LINQ select(x => new

From Dev

C# Linq Column Name as variable

From Dev

Column Name from string variable linq query

From Dev

Using EF and Linq and exclude properties in a select statement

From Dev

Using a variable as a column name in rails

Related Related

  1. 1

    Linq using variable as column name in select new statement

  2. 2

    Finding column name and using it in select statement

  3. 3

    Simplify IF-clause and find name of Variable in LINQ select statement

  4. 4

    using a IF type statement in a linq to entities SELECT NEW clause C#

  5. 5

    Using a function inside of a select statement to make a new column

  6. 6

    Dynamically select column name using Linq to Entities (EF6)

  7. 7

    Invalid Column Name in Select Statement

  8. 8

    Invalid Column Name in Select Statement

  9. 9

    Variable table name in select statement

  10. 10

    How to select column name in select statement

  11. 11

    SQL column name same as PL/SQL variable name - How can this be done in a select statement?

  12. 12

    SQL column name same as PL/SQL variable name - How can this be done in a select statement?

  13. 13

    LINQ Dynamically Select Column From Column Name

  14. 14

    Change output property name in linq select statement

  15. 15

    Subset a data.frame using a variable for the column name in the select expression

  16. 16

    Using a variable as the column name

  17. 17

    Using Select and Where in a List using LINQ statement

  18. 18

    Dynamic ORACLE SQL - How to convert a varchar2 variable to use as a column name in a select statement?

  19. 19

    Passing a column name in a SELECT statement in Python

  20. 20

    Change column name in Oracle in SELECT statement

  21. 21

    Ambiguous column name in rawQuery select statement

  22. 22

    How to add new column in select statement

  23. 23

    Table Name Variable in select statement in sql server

  24. 24

    C# How do I use a variable in a Linq select statement using Query Syntax

  25. 25

    Declare variable within LINQ select(x => new

  26. 26

    C# Linq Column Name as variable

  27. 27

    Column Name from string variable linq query

  28. 28

    Using EF and Linq and exclude properties in a select statement

  29. 29

    Using a variable as a column name in rails

HotTag

Archive