how to get value of a definite Column name in c# / Linq?

后端 未结 3 1412
后悔当初
后悔当初 2020-12-20 05:23

I want to know if it possible to get value of a definite columns name ? For exemple SELECT NAME FROM PERSON;

string NAME; <--- Column name
st         


        
3条回答
  •  难免孤独
    2020-12-20 06:05

    You want to the source code in C# to create a linq query that compiles to SELECT NAME FROM PERSON under Linq to SQL or Linq to Entity Framework?

    IEnumerable names = from x in context.PERSONS
                                select x.Name;
    

    OR

    IEnumerable names = context.PERSONS.Select(x => x.Name);
    

    In Monad parlance you want a projection onto the Name property.

    EDIT : You want to dynamically state which column?

    string columnName = "Name";
    ParameterExpression personExpression = Expression.Parameter(typeof(Person), "p");
    Expression> column = Expression.Lambda>(Expression.PropertyOrField(personExpression, columnName), personExpression);
    
    IEnumerable things = context.PERSONS.Select(column);
    

提交回复
热议问题