Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PersonDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Person> persons = Person.GetPerson();
- List<Fruit> fruits = Fruit.GetFruit();
- //1. Show the persons with assets of over 50B dollars.
- var q1 =
- from p in persons
- where p.Asset > 50
- select p;
- Console.WriteLine("Assets over 50B \n");
- foreach (var p in q1)
- Console.WriteLine(p);
- // 2.Show all non-US citizens.
- var q2 =
- from p in persons
- where p.Country != "US"
- select p;
- Console.WriteLine("Non-US Citizen\n");
- foreach (var p in q2)
- Console.WriteLine(p);
- // 3. Show all females from India.
- var q3 =
- from p in persons
- where p.Country == "India" && p.IsFemale
- select p.Name;
- Console.WriteLine("Indian Female\n");
- foreach (var p in q3)
- Console.WriteLine(p);
- // 4. Show all persons whose first name is less than five letters long.
- var q4 =
- from p in persons
- where p.Name.Split()[0].Length < 5
- select p;
- Console.WriteLine("First name less than five letters\n");
- foreach (var p in q4)
- Console.WriteLine(p);
- // 5. Sort the collection by assets.
- var q5 =
- from p in persons
- orderby p.Asset
- select p;
- Console.WriteLine("Sorting by asset\n");
- foreach (var p in q5)
- Console.WriteLine(p.Name + "\t" + p.Asset);
- // 6. Group the collection by country
- var q6 =
- from p in persons
- group p
- by p.Country;
- Console.WriteLine("Grouping by country\n");
- foreach (var aGroup in q6)
- {
- Console.WriteLine("{0} ({1})",
- aGroup.Key, aGroup.Count());
- foreach (var item in aGroup)
- Console.WriteLine(" " + item);
- }
- // 7. Sort the above grouping.
- var q7 =
- from p in persons
- orderby p.Country
- group p
- by p.Country;
- Console.WriteLine("Sort by country\n");
- foreach (var aGroup in q7)
- {
- Console.WriteLine("{0} ({1})",
- aGroup.Key, aGroup.Count());
- foreach (var item in aGroup)
- Console.WriteLine(" " + item);
- }
- // 8. Own queries
- var q8 =
- from p in persons
- where p.Country == "India" && p.Name.Split()[0].Length < 5
- select p;
- Console.WriteLine("First name less than five letters\n");
- foreach (var p in q8)
- Console.WriteLine(p);
- var q9 =
- from p in persons
- orderby p.Asset descending
- select p.Name;
- Console.WriteLine("Sort descending by Asset\n");
- foreach (var p in q9)
- Console.WriteLine(p);
- //LINQ - METHOD SYNTAX
- // 1. Show all non-US citizens
- var p1 = persons.Select(p => p);
- p1 = persons.Where(p => p.Country != "US");
- Console.WriteLine("All Non-US Citizen\n");
- foreach (var p in p1)
- Console.WriteLine(p);
- // 2. Shows only the name of all US citizens
- var p2 = persons.Select(p => p);
- p2 = persons.Where(p => p.Name == "US");
- Console.WriteLine("Name of all US citizen\n");
- foreach (var p in p2)
- Console.WriteLine(p);
- // 3. Show all females from India
- var p3 = persons.Select(p => p);
- p3 = persons.Where(p => p.IsFemale && p.Country == "India");
- Console.WriteLine("Name of all US citizen\n");
- foreach (var p in p3)
- Console.WriteLine(p);
- // 4. Sort the collection by last name and then first name
- var p4 = persons.Select(p => p);
- p4 = persons.OrderBy(p => p.Name.Split()[1]);
- Console.WriteLine("Ordered names by last name first\n");
- foreach (var p in p4)
- Console.WriteLine(p);
- // 5. Group the collection by gender
- var p5 = persons.GroupBy(p => p.IsFemale);
- Console.WriteLine("Grouped collection by Gender\n");
- foreach (var grp in p5)
- {
- Console.WriteLine($"{(grp.Key ? "Female" : "Male")} persons");
- foreach (var item in grp)
- {
- Console.WriteLine(item);
- }
- }
- Console.ReadLine();
- // 6. The first longest word in the string array words
- var p6 = persons.Select(p => p.Name.Split()[0]);
- var word = p6.Max();
- Console.WriteLine("First Longest Word\n" + word);
- Console.ReadLine();
- // 7. The first word with the most vowels
- string[] words = "the quick brown fox jumps over the lazy dog".Split();
- string vowels = "aeiou";
- var p7 = words.Select(p => p);
- Console.WriteLine("A word with most vowels\n");
- int x = 0;
- var abc = "";
- foreach (var item in p7)
- {
- if (item.Count(p => vowels.Contains(p)) > x)
- {
- x = item.Count(p => vowels.Contains(p));
- abc = item;
- }
- }
- Console.WriteLine(abc);
- Console.ReadLine();
- // 8. All the elements in second and third with no duplicates. Do not use the Distinct() method
- string[] second = "a b c e".Split();
- string[] third = "a c d e".Split();
- var v8 = second.Union(third);
- Console.WriteLine("All the elements in second and third with no duplicates\n");
- foreach (var item in v8)
- {
- Console.WriteLine(item);
- }
- Console.ReadLine();
- // 9. Join on persons and fruits. (You may use a mixed-query).
- var p9 = from p in persons
- join f in fruits
- on p.Country equals f.Origin
- select new { Person = p.Name, Fruit = f.Name };
- Console.WriteLine("\n\nInner join.");
- foreach (var item in p9)
- Console.WriteLine($"{item.Person + ":"}{item.Fruit}");
- Console.ReadLine();
- // 10. Any other method that is not describe above.
- var p10 = from p in persons
- from f in fruits
- select new { Person = p.Country, Fruit = f.Name };
- Console.WriteLine("\n\nCross Join.");
- foreach (var item in p10)
- Console.WriteLine($"{item.Person + ":"}{item.Fruit}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement