//LINIQ的操作
using System;
using System.Linq;
using System.Data;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Numerics;
using System.Reflection;
namespace Program
{
public class program
{
public static void Main()
{
#region 表
List<User> list = new List<User>()
{
new User { id = 1, name = "Zhang Long", age = 38, gender = true, occupation = "Teacher"},
new User { id = 2, name = "Zhang Jin", age = 18, gender = false, occupation = "Student"},
new User { id = 3, name = "Zhang Shuai", age = 38, gender = false, occupation = "Teacher"},
new User { id = 4, name = "Liu Guangzhi", age = 38, gender = false, occupation = "Doctor"},
new User { id = 5, name = "Liu Ziming", age = 38, gender = true, occupation = "Doctor"},
new User { id = 6, name = "Liu Shuai", age = 29, gender = false, occupation = "Doctor"},
new User { id = 7, name = "Liu Jin", age = 21, gender = true, occupation = "Builder"},
new User { id = 8, name = "Jiang Long", age = 38, gender = true, occupation = "Builder"},
new User { id = 9, name = "Hu Ziming", age = 21, gender = true, occupation = "Student"},
new User { id = 10, name = "Hu Jin", age = 21, gender = false, occupation = "Student"}
};
List<Salary> salarylist = new List<Salary>()
{
new Salary { id = 1, user_id = 1, name = "Zhang Long", salary = 7800, active = true, occupation = "Teacher"},
new Salary { id = 2, user_id = 2, name = "Zhang Jin", salary = 1500, active = true, occupation = "Student"},
new Salary { id = 3, user_id = 3, name = "Zhang Shuai", salary = 8800, active = false, occupation = "Teacher"},
new Salary { id = 4, user_id = 4, name = "Liu Guangzhi", salary = 12800, active = true, occupation = "Doctor"},
new Salary { id = 5, user_id = 5, name = "Liu Ziming", salary = 13600, active = true, occupation = "Doctor"},
new Salary { id = 6, user_id = 6, name = "Liu Shuai", salary = 29000, active = false, occupation = "Doctor"},
new Salary { id = 7, user_id = 7, name = "Liu Jin", salary = 7000, active = true, occupation = "Builder"},
new Salary { id = 8, user_id = 8, name = "Jiang Long", salary = 8500, active = false, occupation = "Builder"},
new Salary { id = 9, user_id = 9, name = "Hu Ziming", salary = 2100, active = true, occupation = "Student"},
new Salary { id = 10, user_id = 10, name = "Hu Jin", salary = 1300, active = true, occupation = "Student"}
};
#endregion
#region 基础分组
/*IEnumerable<IGrouping<string, User>> UserGroupByOccupation = list.GroupBy(s => s.occupation);
foreach (var user in UserGroupByOccupation)
{
Console.WriteLine(user.Key);
foreach (var item in user)
{
Console.WriteLine("id:" + item.id + " name:"+ item.name);
}
Console.WriteLine();
}*/
#endregion
#region 两个属性分组
/*IEnumerable<ListMultiGroupResult> UserGroupByOccupationAndGender =
list.
GroupBy(s => new { s.occupation, s.gender }).
Select(g => new ListMultiGroupResult()
{
Occupation = g.Key.occupation,
Gender = g.Key.gender,
Users = g.ToList()
});
foreach(var g in UserGroupByOccupationAndGender)
{
Console.WriteLine(g.Gender+" "+ g.Occupation);
foreach(var item in g.Users)
{
Console.WriteLine(item.id+" "+ item.name);
}
Console.WriteLine();
}*/
#endregion
#region 分组求值
/*IEnumerable<AgeGroupResult> result = list.GroupBy(s => s.occupation).
Select(g => new AgeGroupResult()
{
Occupation=g.Key,//与上类比 g.key.属性只有两个属性进行分组才会使用
MaxAge=g.Max(g=>g.age),
MinAge=g.Min(g=>g.age),
AvgAge=g.Average(g=>g.age),
SumAge=g.Sum(g=>g.age)
});
foreach (var g in result)
{
Console.WriteLine(g.Occupation);
Console.WriteLine(g.MinAge);
Console.WriteLine(g.MaxAge);
Console.WriteLine(g.AvgAge);
Console.WriteLine(g.SumAge);
}*/
#endregion
List<User> users = list.Where(g => g.age > 25).
Select(g=>new User()
{
name= g.name,
age= g.age,
occupation= g.occupation
}).ToList();
foreach (User user in users)
{
Console.WriteLine(user.name+" == "+user.age);
}
List<User> salary_list = salarylist.Where(u => u.salary > 17000).
Select(g => new User()
{
name = g.name,
occupation= g.occupation
}).ToList();
List<User> result_list=users.Intersect(salary_list,new CompareUser()).ToList();
foreach(var g in result_list)
{
Console.WriteLine("id:" + g.id + " name:" + g.name);
}
//var lista = salary_list.Where(s => s.occupation == "Doctor").Select(u => (Name: u.name, Age: u.age));
int x = 1;
}
#region 打印测试
public static void PrintList(List<string> nameList)
{
foreach (string name in nameList)
{
Console.WriteLine(name);
}
}
#endregion
}
class CompareUser:IEqualityComparer<User>//接口就是一个"协议"
{
public bool Equals(User x, User y)
{
if(x.name==y.name&&x.occupation.ToLower()==y.occupation.ToLower())//字符串变小写
return true;
return false;
}
public int GetHashCode(User obj)
{
return obj.name.Length+obj.occupation.Length;
}
}
#region 分组测试类
class AgeGroupResult
{
public string Occupation { get; set; }
public int MaxAge { get; set; }
public int MinAge { get; set; }
public double AvgAge { get; set; }
public int SumAge { get; set; }
}
class ListMultiGroupResult
{
public string Occupation { get; set; }
public bool Gender { get; set; }
public List<User> Users { get; set; }
}
class User
{
public int id { get; set; }
public string name { get; set; }
public bool gender { get; set; }//male: true; female: fasle
public int age { get; set; }
public string occupation { get; set; } //职业
}
class Salary
{
public int id { get; set; }
public int user_id { get; set; }
public string name { get; set; }
public double salary { get; set; }
public bool active { get; set; }
public string occupation { get; set; } //职业
}
#endregion
}