【发布时间】:2013-12-14 21:37:05
【问题描述】:
如何使用 linq 表达式通过匿名方法获取单列。这是我的代码,它不起作用:
public IEnumerable<object> GetPropertyValues<T>(string propName) where T : class
{
return base.Query<T>().AsEnumerable()
.Where(x => x.GetType().GetProperty(propName).Name == propName)
.Select(x => x.GetType().GetProperty(propName).GetValue(x, null));
}
这是非泛型方法中的代码:
base.Query<Product>().Select(x => x.ProductName).AsEnumerable();
提前致谢。
【问题讨论】:
-
“它不起作用”并没有给我们任何有用的信息。你的
Where子句有什么意义?GetProperty(propName)只会返回具有该名称的属性...这就是重点。 -
他可能正在尝试做
base.Query<Product>().Pluck("ProductName").AsEnumerable(); -
我的意思是像 SQL 中的
Select ProductName [Name] From Product或base.Query<Product>().Select(x => new { Name = x.ProductName }).AsEnumerable();
标签: c# linq anonymous-methods