如何让C#/ LINQ的一个明确列名的价值? [英] how to get value of a definite Column name in c# / Linq?
问题描述
我想知道是否有可能得到一个明确的列名的价值?
。对于为例 SELECT NAME由人;
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
string fundPerson;
fundPerson = context.PERSON.Where(a => a... == NAME);
的问题是,在列名可以改变的,它并不总是名称栏。
因此,我需要一个解决方案,我可以动态传递列名到查询。
The problem is that the column name can change, it is not always the "NAME" column. Hence I need a solution where I can dynamically pass a column name to the query.
推荐答案
您想要的在C#源代码来创建一个LINQ查询编译为 SELECT NAME由人
的LINQ下SQL或LINQ到实体框架?
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<string> names = from x in context.PERSONS
select x.Name;
或
IEnumerable<string> names = context.PERSONS.Select(x => x.Name);
在Monad的说法,你想有一个投影到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<Func<Person, string>> column = Expression.Lambda<Func<Person, string>>(Expression.PropertyOrField(personExpression, columnName), personExpression);
IEnumerable<string> things = context.PERSONS.Select(column);
这篇关于如何让C#/ LINQ的一个明确列名的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!