1

Basically I have a collection of sizes.. Collection

12,12
23,23
34,34
23,65
12,3

etc..

I am trying to take these and compare the 2 values and return a string..

If the values are the same, then return only 1 of the numbers, if they are different then return both..

Example..

  new string.. 12, 23, 34, 23x65, 12x3

This is the code I wrote which obviously is not the result I am trying to get..

 List<double[]> oSize_list = _orderedCollection
    .Select(t => new double[] { t.psizeW, t.psizeH })
    .ToList();
4

3 に答える 3

7
List<string> oSize_list = _orderedCollection
    .Select(t => t.psizeW == t.psizeH ? t.psizeW.ToString() : string.Format("{0}x{1}", t.psizeW, t.psizeH))
    .ToList();

これはあなたの目標を達成するはずです

于 2013-03-19T19:31:26.567 に答える
5

equals(psizeW, psizeH)かどうかに応じた文字列形式で、ペアの配列を文字列の配列に変換するには、次のようにします。psizeWpsizeH

var result = _orderedCollection
    .Select(t => t.psizeW == t.psizeH ? 
            string.Format("{0}", t.psizeW) :
            string.Format("{0}x{1}", t.psizeW, t.psizeH))
    .ToList();
于 2013-03-19T19:31:19.310 に答える
3

最も簡単な方法は、比較を行う関数を作成し、それを linq クエリで使用することです。

    private string SizeToString(int a, int b)
    {
        if (a == b)
        {
            return a.ToString();
        }
        else
        {
            return String.Format("{0}x{1}", a, b);
        }
    }


    var stringSizes = from t in _orderedCollection
                      select SizeToString(t.psizeW, t.psizeH);

同じオブジェクト タイプに対して常にこれを実行したい場合は、SizeToString が個々の寸法ではなくサイズ オブジェクトを取るようにすることができます。

于 2013-03-19T19:42:10.090 に答える