Можно ли как-то преобразовать всю коллекцию, а не делать это по одному? ⇐ C#
-
Anonymous
Можно ли как-то преобразовать всю коллекцию, а не делать это по одному?
Is it somehow possible to transform an entire collection instead of doing them one by one?
I am often in a situation where I need to convert elements in a list from one type to another type. The solution i usually end up with is something like this
using System; using System.Collections.Generic; using System.Linq; namespace ListProcessing { public class Person { public int Id { get; set; } public string Name { get; set; } public string Department { get; set; } } public class Employee { public int Id { get; set; } public string Name { get; set; } public string Position { get; set; } } class Program { public static List Hire(List persons) { var output = new List(); foreach(var p in persons) { var employee = new Employee { Id = p.Id, Name = p.Name, Position = "Software Engineer" }; } return output; } static void Main(string[] args) { List persons = new List { new Person { Id = 1, Name = "John Doe", Department = "Software" }, new Person { Id = 2, Name = "Jane Smith", Department = "Marketing" }, new Person { Id = 3, Name = "Bob Johnson", Department = "Software" }, new Person { Id = 4, Name = "Sally Jones", Department = "HR" } }; //Attempt 1 IEnumerable employees = persons .Select(p => new Employee { Id = p.Id, Name = p.Name, Position = "Software Engineer" }); //Attempt 2 var employees2 = Hire(persons); foreach (Employee employee in employees) { Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } } } } Either doing it by linq or by inserting each element into a list and returning them.
Some sort of iteration is always needed, and the fact the same instruction has to be done foreach item is what annoys me..
I sort of feel like this could be solved either using the right type from the beginning, or some sort of overload mechanism could be implmented that does not iterate them, but basically process the collection as a collection and returns the same collection "processed" - and O(1) operation.
Источник: https://stackoverflow.com/questions/780 ... ing-them-o
Is it somehow possible to transform an entire collection instead of doing them one by one?
I am often in a situation where I need to convert elements in a list from one type to another type. The solution i usually end up with is something like this
using System; using System.Collections.Generic; using System.Linq; namespace ListProcessing { public class Person { public int Id { get; set; } public string Name { get; set; } public string Department { get; set; } } public class Employee { public int Id { get; set; } public string Name { get; set; } public string Position { get; set; } } class Program { public static List Hire(List persons) { var output = new List(); foreach(var p in persons) { var employee = new Employee { Id = p.Id, Name = p.Name, Position = "Software Engineer" }; } return output; } static void Main(string[] args) { List persons = new List { new Person { Id = 1, Name = "John Doe", Department = "Software" }, new Person { Id = 2, Name = "Jane Smith", Department = "Marketing" }, new Person { Id = 3, Name = "Bob Johnson", Department = "Software" }, new Person { Id = 4, Name = "Sally Jones", Department = "HR" } }; //Attempt 1 IEnumerable employees = persons .Select(p => new Employee { Id = p.Id, Name = p.Name, Position = "Software Engineer" }); //Attempt 2 var employees2 = Hire(persons); foreach (Employee employee in employees) { Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); } } } } Either doing it by linq or by inserting each element into a list and returning them.
Some sort of iteration is always needed, and the fact the same instruction has to be done foreach item is what annoys me..
I sort of feel like this could be solved either using the right type from the beginning, or some sort of overload mechanism could be implmented that does not iterate them, but basically process the collection as a collection and returns the same collection "processed" - and O(1) operation.
Источник: https://stackoverflow.com/questions/780 ... ing-them-o
Мобильная версия