Is it possible in C# to do something similar to the following?
var tigerlist = new List<Tigers>(){ Tail = 10, Teeth = 20 };
var tigers_to_cats_approximation = new List<Cat>()
{
foreach (var tiger in tigerlist)
{
new Cat()
{
Tail = tiger.Tail / 2,
Teeth = tiger.Teeth / 3,
HousePet = true,
Owner = new Owner(){ name="Tim" }
}
}
}
I'm doing some XML apis and the request objects coming in are similar to the response objects that need to go out. The above would be incredibly convenient if it were possible; much more so than auto-mapper.
var tigers_to_cats_approximation = tigerlist.Select(tiger => new Cat() { Tail = tiger.Tail / 2, Teeth = tiger.Teeth / 3, HousePet = true, Owner = new Owner(){name="Tim"} }).ToList();
? – EfrenTiger
and returns aCat
(i.e.public Cat ConvertToCat(Tiger tiger) { return new Cat() { Tail = tiger.Tail / 2, Teeth = tiger.Teeth / 3, HousePet = true, Owner = new Owner() { name="Tim" } }; }
), which you could then call using aSelect
statement on thetigerList
, like:var catsFromTigers = tigerList.Select(tiger => ConvertToCat(tiger)).ToList();
– SolicitortigerList.Select(ConvertToCat).ToList();
– Efren