Skip to content

Instantly share code, notes, and snippets.

@DapperFox
Created August 14, 2013 00:18
Show Gist options
  • Select an option

  • Save DapperFox/6226957 to your computer and use it in GitHub Desktop.

Select an option

Save DapperFox/6226957 to your computer and use it in GitHub Desktop.
Final with comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab3
{
class Program
{
static void Main(string[] args)
{
//1. Print out all people ordered by zip
var data = new DataManager();
Console.WriteLine("QUESTION 1");
foreach (var s in data.AllPeople.OrderBy((person) => person.ZipCode))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//2. Print out all people ordered by last name, then by first name
Console.WriteLine("QUESTION 2");
foreach (var s in data.AllPeople.OrderBy((person) => person.LastName).ThenBy((person) => person.FirstName))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//3. Print out all people with zip codes greater than 50000,
// ordered by their social security numbers
Console.WriteLine("QUESTION 3");
foreach (var s in data.AllPeople.Where(person => person.ZipCode > 50000).OrderBy(person => person.SocialSecurity))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//4. Get a reference to the person with an invalid zip code (greater than 99999), or none if no one exists.
Console.WriteLine("QUESTION 4");
var myPerson = data.AllPeople.FirstOrDefault(person => person.ZipCode > 99999);
Console.WriteLine(myPerson);
Console.WriteLine();
//5. Print out all people with either the first or last name “Washington”
Console.WriteLine("QUESTION 5");
foreach (var s in data.AllPeople.Where((person) => person.LastName == "Washington" || person.FirstName == "Washington"))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//6. Print out all people who have the letter ‘i’ in both their first and last names.
Console.WriteLine("QUESTION 6");
foreach (var s in data.AllPeople.Where((person) => person.LastName.Contains("i") && person.FirstName.Contains("i")))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//7. Print out all people with social security numbers starting with 1,2,3,4, or 5 ordered by their
// first name, followed by all people with social security numbers starting with 6,7,8.9, or 0 ordered
// by their last name. Use one foreach loop for this.
Console.WriteLine("QUESTION 7");
foreach (var s in data.AllPeople.
OrderBy(person => person.FirstName).
Where(person => int.Parse(person.SocialSecurity.ElementAt(0).ToString()) <= 5 && int.Parse(person.SocialSecurity.ElementAt(0).ToString()) > 0).
Union(data.AllPeople.
OrderBy(person => person.LastName).
Where(person => int.Parse(person.SocialSecurity.ElementAt(0).ToString()) > 5 || int.Parse(person.SocialSecurity.ElementAt(0).ToString()) == 0)))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//8. Print out the number of people who have the letter ‘a’ in their first name
Console.WriteLine("QUESTION 8");
Console.WriteLine(data.AllPeople.Count(person => person.FirstName.Contains("a")));
Console.WriteLine();
//9. Print out all people with social security numbers beginning with 1, 3, or 5,
// ordered by their first name.
Console.WriteLine("QUESTION 9");
foreach (var s in data.AllPeople.
OrderBy(person => person.FirstName).
Where(person => int.Parse(person.SocialSecurity.ElementAt(0).ToString())%2 == 1 && int.Parse(person.SocialSecurity.ElementAt(0).ToString()) <= 5))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
//10. Print out all people with the number 3 in their zip code.
Console.WriteLine("QUESTION 10");
foreach (var s in data.AllPeople.Where(person => person.ZipCode.ToString().Contains("3")))
{
Console.WriteLine(s.ToString());
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment