Lets say I had my object:
public class MyObject{ public p1; public p2; }
And the enumerable version:
IEnumerable<MyObject> MyObjects;
How would I turn it into a string[]
that would be an array of p1 + '=' + p2
values?
You could do some simple linq on it
(from m in MyObjects
select m.p1 + "=" + p2).ToArray()
if you don't really need it as an array you can omit the call to ToArray()
and you'd then have an IEnumerable<string>
instead
If you wish to put them all into a comma separated list you don't need to transform to an array since the string.Join
method has an over load that takes an enumerable. So you can do this:
string.Join(",",from m in MyObjects
select m.p1 + "=" + p2);
I personally prefer the above syntax but if you are all for using fewer letters then the code is semantically equal to the below shorter snippet
string.Join(",",MyObjects.Select(m => m.p1 + "=" + p2));
MyObjects.Select(o => o.p1 + "=" + o1.p2).ToArray()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.