Reputation: 375
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();
Upvotes: 1
Views: 459
Reputation: 2416
The easiest thing to do is to create a function to do your comparison and use it in your linq query.
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);
If you're always wanting to do this for the same object type, you could make SizeToString take a size object instead of the individual dimensions.
Upvotes: 3
Reputation: 82624
List<string> oSize_list = _orderedCollection
.Select(t => t.psizeW == t.psizeH ? t.psizeW.ToString() : string.Format("{0}x{1}", t.psizeW, t.psizeH))
.ToList();
This should accomplish your goal
Upvotes: 7
Reputation: 238166
To turn an array of (psizeW, psizeH)
pairs into an array of strings, with the string format depending on whether psizeW
equals psizeH
, you could:
var result = _orderedCollection
.Select(t => t.psizeW == t.psizeH ?
string.Format("{0}", t.psizeW) :
string.Format("{0}x{1}", t.psizeW, t.psizeH))
.ToList();
Upvotes: 5