动态选择要使用Linq获得的属性 [英] Dynamically choose which properties to get using Linq

查看:109
本文介绍了动态选择要使用Linq获得的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序,其中一个页面上有一个动态表,用户可以定义该表具有多少列,各列的顺序以及从何处获取每个字段的数据.

I have an MVC application with a dynamic table on one of the pages, which the users defines how many columns the table has, the columns order and where to get the data from for each field.

为了保持动态性,我编写了一些非常糟糕的代码,现在我希望它效率更高.

I have written some very bad code in order to keep it dynamic and now I would like it to be more efficient.

我的问题是我不知道如何定义在运行时应该返回到IEnumerable的列.我的主要问题是我不知道我可能有多少列.

My problem is that I don't know how to define the columns I should get back into my IEnumerable on runtime. My main issue is that I don't know how many columns I might have.

我有一个获取该字段文本的类的引用.我也有一个具有确切属性的每个字段顺序的字典,它应该从中获取数据.

I have a reference to a class which gets the field's text. I also have a dictionary of each field's order with the exact property It should get the data from.

我的代码应如下所示:

var docsRes3 = from d in docs
                       select new[] 
                       {  
                           for (int i=0; i<numOfCols; i++)
                           {
                               gen.getFieldText(d, res.FieldSourceDic[i]);
                           }
                       };

其中:
docs =我仅想从中获取特定字段的列表
res.FieldSourceDic =词典,其中键是列的顺序,值是属性
gen.getFieldText =该函数获取实体和属性并返回值

where:
docs = List from which I would like to get only specific fields
res.FieldSourceDic = Dictionary in which the key is the order of the column and the value is the property
gen.getFieldText = The function which gets the entity and the property and returns the value

显然,它不起作用.

我也尝试过

StringBuilder fieldsSB = new StringBuilder();
        for (int i = 0; i < numOfCols; i++)
        {
            string field = "d." + res.FieldSourceDic[i] + ".ToString()";
            if (!string.IsNullOrEmpty(fieldsSB.ToString()))
            {
                fieldsSB.Append(",");
            }
            fieldsSB.Append(field);
        }


        var docsRes2 = from d in docs
                       select new[] { fieldsSB.ToString() };

它也不起作用.

到目前为止,唯一对我有用的是:

The only thing that worked for me so far was:

List<string[]> docsRes = new List<string[]>();
        foreach (NewOriginDocumentManagment d in docs)
        {
            string[] row = new string[numOfCols];

            for (int i = 0; i < numOfCols; i++)
            {
                row[i] = gen.getFieldText(d, res.FieldSourceDic[i]);
            }

            docsRes.Add(row);
        }

有什么主意我该如何传递linq字段列表,这样才能有效地从中切出所需的数据?

Any idea how can I pass the linq the list of fields and it'll cut the needed data out of it efficiently?

谢谢,e,我很清楚自己的需求....

Thanks, Hoe I was clear about what I need....

推荐答案

在以下链接的帮助下,我得到了答案: http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ

I got my answer with some help from the following link: http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ

首先,我创建了一个包含所有属性的字符串数组:

First I created a string array of all properties:

//Creats a string of all properties as defined in the XML
        //Columns order must be started at 0. No skips are allowed
        StringBuilder fieldsSB = new StringBuilder();
        for (int i = 0; i < numOfCols; i++)
        {
            string field = res.FieldSourceDic[i];
            if (!string.IsNullOrEmpty(fieldsSB.ToString()))
            {
                fieldsSB.Append(",");
            }
            fieldsSB.Append(field);
        }
        var cols = fieldsSB.ToString().Split(',');

        //Gets the data for each row dynamically
        var docsRes = docs.Select(d => GetProps(d, cols));

然后我创建了GetProps函数,该函数使用了我自己的函数,如问题所述:

than I created the GetProps function, which is using my own function as described in the question:

private static dynamic GetProps(object d, IEnumerable<string> props)
    {
        if (d == null)
        {
            return null;
        }

        DynamicGridGenerator gen = new DynamicGridGenerator();
        List<string> res = new List<string>();

        foreach (var p in props)
        {
            res.Add(gen.getFieldText(d, p));
        }

        return res;
    }

这篇关于动态选择要使用Linq获得的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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