I find myself always needing to thin out objects before sending them over the wire.
Background:
Position
is a heavy contender, which was generated by LINQ to SQL based off my table. It stores the motherlode of data.
SPosition
is a lightweight object, which stores only my latitude and longitide.
Code:
List<SPosition> spositions = new List<SPosition>();
foreach (var position in positions) // positions = List<Position>
{
SPosition spos = new SPosition { latitude = position.Latitude, longitude = position.Longitude };
spositions.Add(spos);
}
return spositions.SerializeToJson<List<SPosition>>();
How can I use some LINQ magic to clean this up a bit?
var spositions = positions.Select(
position => new SPosition
{
latitude = position.Latitude,
longitude = position.Longitude
}).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With