2017年3月23日 星期四

[Linq] 練習

 protected void Page_Load(object sender, EventArgs e)
        {

            var people = GenerateListOfPeople();

            //There will be two Persons in this variable: the "Steve" Person and the "Jane" Person
            var peopleOverTheAgeOf30 = people.Where(x => x.Age > 30);//設定條件
            foreach (var person in peopleOverTheAgeOf30)
            {
              Label1.Text=  person.FirstName;
            }
        }
        public static void Main()
        {
            var people = GenerateListOfPeople();

            // Write your code here
        }

        public static List<Person> GenerateListOfPeople()//資料庫
        {
            var people = new List<Person>();

            people.Add(new Person { FirstName = "Eric", LastName = "Fleming", Occupation = "Dev", Age = 24 });
            people.Add(new Person { FirstName = "Steve", LastName = "Smith", Occupation = "Manager", Age = 40 });
            people.Add(new Person { FirstName = "Brendan", LastName = "Enrick", Occupation = "Dev", Age = 30 });
            people.Add(new Person { FirstName = "Jane", LastName = "Doe", Occupation = "Dev", Age = 35 });
            people.Add(new Person { FirstName = "Samantha", LastName = "Jones", Occupation = "Dev", Age = 24 });

            return people;
        }
 

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Occupation { get; set; }
        public int Age { get; set; }
    }


資料來源:https://www.microsoft.com/net/tutorials/csharp/getting-started/linq

沒有留言:

張貼留言