Linq使用包含字段名称的字符串选择单个字段 [英] Linq select a single field using a string containing the name of the field

查看:306
本文介绍了Linq使用包含字段名称的字符串选择单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简单的对象列表,如下所示:

Let's say I have a simple list of objects like this:

public class DataField
{
    public int DataFieldId {get; set;}
    public int KeyId {get; set;}
    public string FieldName {get; set;}
    public string Data {get; set;}
}

现在,我想使用属性名称的字符串值来获取属性中值的列表,如下所示:

Now I would like to get a list of the values in a Property using the string value of the Property name, like this:

public List<string> getFieldData(List<DataField> dataToSearch, string propertyName)
{
    // This is the area I'd like to figure out.
    return dataToSearch.Select(ds => ds.propertyName).Distinct.ToList();
}

public void MyMethod()
{
    var data = new List<DataField>{
        new DataField{DataFieldId = 1, KeyId = 1,
            FieldName = "UserName", Data = "jSmith"},
        new DataField{DataFieldId = 2, KeyId = 1,
            FieldName = "Email", Data = "jSmith@nowhere.com"},
        new DataField{DataFieldId = 3, KeyId = 1,
            FieldName = "PreferredContact", Data = "Phone"},
        new DataField{DataFieldId = 4, KeyId = 2,
            FieldName = "UserName", Data = "jDoe"},
        new DataField{DataFieldId = 5,KeyId = 2,
            FieldName = "Email", Data = "janeDoe@emailServer.net"},
        new DataField{DataFieldId = 6, KeyId = 2,
            FieldName = "PreferredContact", Data = "Email"}
    };

    // Notice I want to search using a string
    var fieldNames = getFieldData(data, "FieldName");
}

我希望fieldNames是包含以下内容的List<string>:
用户名"
电子邮件"
"PreferredContact"

I would want fieldNames to be a List<string> containing:
"UserName"
"Email"
"PreferredContact"

我想使用字符串指定要返回的列.

I would like to use a string to specify the column to return.

推荐答案

您可以使用反射.您使用的是字段",但该类实际上包含属性,因此请使用反射的GetProperty()方法.如果您改用字段,请使用GetField()

You can use reflection. You're using "field" but the class actually contains properties, so use reflection's GetProperty() method. If you use fields instead, use GetField()

public static List<string> getFieldData(List<DataField> dataToSearch, string fieldName)
{
    // You can use reflection to get information from types at runtime.
    // The property_info variable will hold various data about the field
    // name you pass in (type, name, etc)
    var property_info = typeof(DataField).GetProperty(fieldName);

    // We can then call property_info's GetValue() on an instantiated 
    // object of our class, and it will return the value of that property on that object
    return dataToSearch.Select(ds => Convert.ToString(property_info.GetValue(ds))).Distinct().ToList();
}

PropertyInfo类: https://msdn.microsoft.com/zh-CN/library/system.reflection.propertyinfo(v=vs.110).aspx

PropertyInfo class: https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo(v=vs.110).aspx

类型类别: https://msdn.microsoft.com/zh-CN/library/system.type(v=vs.110).aspx

这篇关于Linq使用包含字段名称的字符串选择单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆