FPGA
FPGA

Reputation: 3875

Linq create a list of some type from a list of another type

suppose i have an

List<SomeObject>
List<AnotherObject>

and an extention method that returns SomeObject Instance when i send it AnotherObject

i need to produce a IList<SomeObject> from an IList<AnotherObject> so without linq i wolud do it as

private List<SomeObject> ToListOfProductEntry(List<AnotherObject> list)
        {
            List<SomeObject> result = new List<SomeObject>();
            foreach (var obj in list)
            {
                result.Add(obj.ToSomeObject()); // ToSomeObject() is an extention method
            }
            return result ;
        }

how can achive the same result using LINQ, i dont use LINQ alot so i am not familiar with it.

Upvotes: 0

Views: 38

Answers (1)

King King
King King

Reputation: 63377

You can use Select projection to shape data for each element.

var result = list.Select(x=>x.ToSomeObject()).ToList();    

Upvotes: 2

Related Questions