using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<Person> people = new List<Person>
{
new Person { Id = 1, Name = "約翰史密斯", Age = 35 },
new Person { Id = 2, Name = "簡道", Age = 28 },
new Person { Id = 3, Name = "約翰強生", Age = 42 },
new Person { Id = 4, Name = "愛麗絲強森", Age = 31 },
new Person { Id = 5, Name = "鮑勃史密斯", Age = 40 },
new Person { Id = 6, Name = "傑克強森", Age = 29 },
};
var query = from person in people
where person.Age >= 30 && person.Name.StartsWith("約翰")
orderby person.Age descending
select new
{
Id = person.Id,
Name = person.Name,
Age = person.Age
};
var result = query.Take(5).ToList();
foreach (var person in result)
{
Console.WriteLine($"編號:{person.Id},姓名:{person.Name},年齡:{person.Age}");
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
留言
張貼留言