namespace ConsoleApplication1
{
public class Student
{
public string FirstName;
public string LastName;
}
public class StudentFullName
{
public string FullName;
public StudentFullName(Student student)
{
FullName = student.FirstName + " " + student.LastName;
}
public override string ToString()
{
return FullName;
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student { FirstName = "Jack", LastName = "Frost" };
Student s2 = new Student { FirstName = "Mary", LastName = "Wong" };
Student s3 = new Student { FirstName = "Ronald", LastName = "McDonalds" };
Student s4 = new Student { FirstName = "John", LastName = "Smith" };
List<Student> studentArray = new List<Student>() { s1, s2, s3, s4 };
List<StudentFullName> fullNameArray = studentArray.Select(a => new StudentFullName(a)).ToList();
string nameList = fullNameArray.OrderBy(m => m.FullName).Select(m => m.FullName).Aggregate((current, next) => current + ", " + next);
Console.WriteLine(nameList);
Console.ReadLine();
}
}
}
04 September 2017
Array mapping using LINQ
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment