C# 如何在匿名方法中使用LINQ获取单列

C# 如何在匿名方法中使用LINQ获取单列,c#,linq,anonymous-methods,C#,Linq,Anonymous Methods,如何使用linq表达式使用匿名方法获取单个列。这是我的代码,但它不起作用: public IEnumerable<object> GetPropertyValues<T>(string propName) where T : class { return base.Query<T>().AsEnumerable() .Where(x => x.GetType().GetProperty(propName).Name == prop

如何使用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();

提前感谢。

此条件不正确,因为当属性propName丢失时,它会崩溃,而不是返回false:

如果您想说动态类型具有propName属性,则它的适当条件如下所示:

.Where(x => x.GetType().GetProperty(propName) != null)
var theProp = typeof(T)..GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(x => theProp.GetValue(x, null));
请注意,只有当T的某些子类(而不是所有子类)具有所需的属性propName时,才需要这样做。如果属性存在于T本身中,则可以预先获取属性,并按如下方式执行查询的其余部分:

.Where(x => x.GetType().GetProperty(propName) != null)
var theProp = typeof(T)..GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(x => theProp.GetValue(x, null));

此条件不正确,因为当属性propName丢失时,它会崩溃,而不是返回false:

如果您想说动态类型具有propName属性,则它的适当条件如下所示:

.Where(x => x.GetType().GetProperty(propName) != null)
var theProp = typeof(T)..GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(x => theProp.GetValue(x, null));
请注意,只有当T的某些子类(而不是所有子类)具有所需的属性propName时,才需要这样做。如果属性存在于T本身中,则可以预先获取属性,并按如下方式执行查询的其余部分:

.Where(x => x.GetType().GetProperty(propName) != null)
var theProp = typeof(T)..GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(x => theProp.GetValue(x, null));

一旦调用了AsEnumerable,所有进一步的过滤都将发生在内存中,而不是SQL中。因此,您可以:

var prop = typeof(T).GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(t => prop.GetValue(t));
但这将选择T的所有列进入内存,然后再过滤到一列。要在SQL中执行筛选,需要动态构造表达式:

var prop = typeof(T).GetProperty(propName);
var parameter = Expression.Parameter(typeof(T));
var memberAccess = Expression.MakeMemberAccess(parameter, prop);
var selector = Expression.Lambda<Func<T, TProperty>>(memberAccess, parameter);
return base.Query<T>().Select(selector);

一旦调用了AsEnumerable,所有进一步的过滤都将发生在内存中,而不是SQL中。因此,您可以:

var prop = typeof(T).GetProperty(propName);
return base.Query<T>().AsEnumerable().Select(t => prop.GetValue(t));
但这将选择T的所有列进入内存,然后再过滤到一列。要在SQL中执行筛选,需要动态构造表达式:

var prop = typeof(T).GetProperty(propName);
var parameter = Expression.Parameter(typeof(T));
var memberAccess = Expression.MakeMemberAccess(parameter, prop);
var selector = Expression.Lambda<Func<T, TProperty>>(memberAccess, parameter);
return base.Query<T>().Select(selector);

对于任何类型对象的枚举

public static IEnumerable<Object> Pluck<Object>(this IEnumerable<Object> source, string propName)
{
    return source
        .Select(x => new {
            obj: x
            prop: x.GetType().GetProperty(propName)
        })
        .Where(x => x.prop != null)
        .Select(x => x.prop.GetValue(x.obj));
};
对于单一类型对象的枚举

public static IEnumerable<Object> Pluck<T>(this IEnumerable<T> source, string, propName)
{
    var prop = typeof(T).GetProperty(propName);
    return prop == null
         ? Enumerable.Empty<Object>()
         : source.Select(x => prop.GetValue(x));
};

对于任何类型对象的枚举

public static IEnumerable<Object> Pluck<Object>(this IEnumerable<Object> source, string propName)
{
    return source
        .Select(x => new {
            obj: x
            prop: x.GetType().GetProperty(propName)
        })
        .Where(x => x.prop != null)
        .Select(x => x.prop.GetValue(x.obj));
};
对于单一类型对象的枚举

public static IEnumerable<Object> Pluck<T>(this IEnumerable<T> source, string, propName)
{
    var prop = typeof(T).GetProperty(propName);
    return prop == null
         ? Enumerable.Empty<Object>()
         : source.Select(x => prop.GetValue(x));
};

它不起作用,不能给我们任何有用的信息。你的Where条款有什么意义?GetPropertypropName将仅返回具有该名称的属性。。。这就是重点。他可能在尝试base.Query.pullProductName.AsEnumerable;我的意思是像在SQL或base.Query.Selectx=>new{Name=x.ProductName}.AsEnumerable中从产品中选择ProductName[Name];它不起作用,不能给我们任何有用的信息。你的Where条款有什么意义?GetPropertypropName将仅返回具有该名称的属性。。。这就是重点。他可能在尝试base.Query.pullProductName.AsEnumerable;我的意思是像在SQL或base.Query.Selectx=>new{Name=x.ProductName}.AsEnumerable中从产品中选择ProductName[Name];我希望获得基于特定属性的所有值。我试过你的建议,但在财产问题上犯了错误。你能解释一下吗?我把TResult改成object,它就工作了。我还有一个问题,我可以这样输入匿名类型吗?base.Query.Selecx=>new{Name=x.ProductName};我希望获得基于特定属性的所有值。我试过你的建议,但在财产问题上犯了错误。你能解释一下吗?我把TResult改成object,它就工作了。我还有一个问题,我可以这样输入匿名类型吗?base.Query.Selecx=>new{Name=x.ProductName};