0

ページの1つに動的テーブルを持つMVCアプリケーションがあります。ユーザーは、テーブルに含まれる列の数、列の順序、および各フィールドのデータをどこから取得するかを定義します。

動的に保つためにいくつかの非常に悪いコードを書きましたが、今はもっと効率的にしたいと思っています。

私の問題は、実行時に IEnumerable に戻す必要がある列を定義する方法がわからないことです。私の主な問題は、列がいくつあるかわからないことです。

フィールドのテキストを取得するクラスへの参照があります。また、データを取得する必要がある正確なプロパティを含む各フィールドの順序の辞書もあります。

私のコードは次のようになります。

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 = エンティティとプロパティを取得する関数値を返します

明らかに、うまくいきません。

私も試しました

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() };

それもうまくいきませんでした。

これまでのところ私にとってうまくいった唯一のことは次のとおりです。

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 にフィールドのリストを渡す方法を教えてください。必要なデータが効率的に切り取られますか?

ありがとう、鍬私は私が必要なものについて明確でした....

4

2 に答える 2

0

次のリンクからいくつかの助けを借りて答えを得ました: http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ

最初に、すべてのプロパティの文字列配列を作成しました。

//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 関数を作成したよりも:

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;
    }
于 2013-04-14T13:28:44.270 に答える
0

以下を試してください:

var docsRes3 = from d in docs
               select ( 
                   from k in res.FieldSourceDic.Keys.Take(numOfCols)
                   select gen.getFieldText(d, res.FieldSourceDic[k]));
于 2013-04-14T12:46:49.927 に答える