1

我试图从动态对象中获取一些特定字段,它实际上是任何类的列表,这个类包含这些字段中的各种字段我想使用 LINQ 选择一些特定字段,我想选择的字段也在传递由用户。下面是我尝试使用 System.Linq.Dynamic 的代码。

using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Collections;

private void Test_Click(object sender, EventArgs e)
{
            List<RateInfo> lst = new List<RateInfo>();
            lst.Add(new RateInfo() { id_country = "IND", id_state = 1, rate = 2.3f });
            lst.Add(new RateInfo() { id_country = "IND", id_state = 2, rate = 1.1f });
            lst.Add(new RateInfo() { id_country = "IND", id_state = 3, rate = 5.2f });
            lst.Add(new RateInfo() { id_country = "IND", id_state = 4, rate = 6.5f });

        GetDynamicData(lst, new List<string>() { "id_country", "id_state" });
}

    private void GetDynamicData(dynamic list, List<string> fetchFields)
    {

        var data = ((IEnumerable)list).Cast<dynamic>()
                        .Select(r => new { r }).AsQueryable();

        StringBuilder s = new StringBuilder();

        //This is for test only. 
        //It works, the value of "id_state" and "id_state" getting appended 
        foreach (var item in data)
        {
            s.Append(item.r.id_state);
            s.Append(",");
            s.Append(item.r.id_country);
        }
        //-----------------------------------------------------


        //Select the specific field data from dynamic list 
        StringBuilder fields = new StringBuilder();

        fields.Append("new (");

        foreach (var fld in fetchFields)
        {
            fields.Append("r." + fld);
            fields.Append(",");
        }
        fields.Remove(fields.Length - 1, 1);
        fields.Append(")");

        //This does not work throws error 
        //"No property or field 'id_country' exists in type 'Object'"
        IQueryable iq = data.Select(fields.ToString());

        //For test only to check the value of selected fields
        foreach (dynamic item in iq)
        {
            s.Append(item.id_state);
            s.Append(",");
            s.Append(item.id_country);
        }

    }
4

2 回答 2

1

您可以极大地简化您的GetDynamicData方法,既指定显式列表类型 ( GetDynamicData(IList<RateInfo> list, ...)) 并保留列表项类型通用,以便重用该方法;考虑到最后一种方法,您可以重写GetDynamicData如下,获得所需的输出:

    private void GetDynamicData<T>(IEnumerable<T> list, List<string> fetchFields)
        {
            var fields = $"new ({string.Join(",", fetchFields)})";
            var res = list.AsQueryable().Select(fields);

            //For test only to check the value of selected fields
            foreach (dynamic item in res) {
                Console.WriteLine(item.id_state);
                Console.WriteLine(item.id_country);
            }

        }

输出

1
IND
2
IND
3
IND
4
IND

解释我认为不同之处在于明确指定类型(通过泛型T或通过RateInfo)你强制LINQ知道列表项的类型;如果您使用实例的 value dynamic,那么查询将失败并出现您遇到的错误。IQueryable.ElementTypeIQuqryableSystem.Object

于 2019-02-20T11:17:26.567 回答
1

您应该尝试使用泛型

private void GetDynamicData<T>(IEnumerable<T> list, List<string> fetchFields)
{

    var data = list.AsQueryable();
于 2019-02-20T10:58:31.073 回答