using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
public partial class Geniric : System.Web.UI.Page
{
/// <summary>
/// 1,遍历泛型集合
/// </summary>
/// <param name="dals"></param>
private void Write(IList<PersonInfo> dals)
{
string tableHead = "<table style='background-color:yellow;'><tr><td>编号</td><td>姓名</td><td>年龄</td><td>性别</td></tr>";
string tableTr = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>";
string tableFoot = "</table>";
Response.Write(tableHead);
foreach (PersonInfo dal in dals)
{
Response.Write(string.Format(tableTr, dal.Id, dal.Name, dal.Age, dal.Sex));
}
Response.Write(tableFoot);
}
/// <summary>
/// 2,根据索引位置移除项
/// </summary>
/// <param name="dals"></param>
/// <param name="index"></param>
private void RemoveAt(IList<PersonInfo> dals, int index)
{
dals.RemoveAt(index);
}
/// <summary>
/// 3,查询一个人
/// </summary>
/// <param name="id">编号</param>
/// <param name="dals"></param>
private void Single(int id, IList<PersonInfo> dals)
{
PersonInfo dal = dals.Single(c => c.Id == 2);
if (dal != null)
{
Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
}
else
{
Response.Write("此编号不存在。");
}
}
/// <summary>
/// 3.2,查询一个人(复合条件查询(同时满足))
/// </summary>
/// <param name="id">编号</param>
/// <param name="name">姓名</param>
/// <param name="dals"></param>
private void Single(int id, string name, IList<PersonInfo> dals)
{
PersonInfo dal = dals.Single(c => c.Id == 2 && c.Name == name);
if (dal != null)
{
Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
}
else
{
Response.Write("此编号不存在。");
}
}
/// <summary>
/// 3.3,查询一个人(复合条件查询(任何一个满足即可))
/// </summary>
/// <param name="id">编号</param>
/// <param name="name">姓名</param>
/// <param name="dals"></param>
private void Single2(int id, string name, IList<PersonInfo> dals)
{
PersonInfo dal = dals.Single(c => c.Id == 2 || c.Name == name);
if (dal != null)
{
Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
}
else
{
Response.Write("此编号不存在。");
}
}
/// <summary>
/// 4,属性列表
/// </summary>
private void Attributes(IList<PersonInfo> dals)
{
Write(dals);
Response.Write("<hr>");
//attr:1,
Response.Write(string.Format("包含项数量:{0}<br>", dals.Count));
var idSum = dals.Sum(p => p.Id);
Response.Write(string.Format("编号总和:{0}<br>", idSum));
var ageAvg = dals.Average(p => p.Age);
Response.Write(string.Format("平均年龄:{0}<br>", ageAvg));
var ageMax = dals.Max(p => p.Age);
Response.Write(string.Format("最大年龄:{0}<br>", ageMax));
var ageMin = dals.Min(p => p.Age);
Response.Write(string.Format("最小年龄:{0}<br>", ageMin));
}
/// <summary>
/// 5,把泛型转为PersonInfo数组集合,并遍历输出
/// </summary>
/// <param name="dals"></param>
private void ToArray(IList<PersonInfo> dals)
{
PersonInfo[] pArray = dals.ToArray();
foreach (PersonInfo p in pArray)
{
Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", p.Id, p.Name, p.Age, p.Sex));
}
}
/// <summary>
/// 6,把泛型转为Dictionary数据词典,并遍历输出
/// </summary>
/// <param name="dals"></param>
private void ToDictionary(IList<PersonInfo> dals)
{
Dictionary<int, PersonInfo> dalsDict = dals.ToDictionary(p => p.Id);
foreach (int key in dalsDict.Keys)
{
PersonInfo dalDict = dalsDict[key];
Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", dalDict.Id, dalDict.Name, dalDict.Age, dalDict.Sex));
}
}
/// <summary>
/// 7,把泛型转为List列表,并遍历输出
/// </summary>
/// <param name="dals"></param>
private void ToList(IList<PersonInfo> dals)
{
List<PersonInfo> dalsList = dals.ToList();
foreach (PersonInfo p in dalsList)
{
Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", p.Id, p.Name, p.Age, p.Sex));
}
}
/// <summary>
/// 8,把泛型转为ILookup,
/// 【没做出】
/// </summary>
/// <param name="dals"></param>
private void ToLookup(IList<PersonInfo> dals)
{
ILookup<int, string> dalsLookup = dals.ToLookup(p => p.Id, p => p.Name);
for (int i = 0; i < dalsLookup.Count; i++)
{
//Response.Write(string.Format("{0}<br>",dalsLookup[i].));
}
}
/// <summary>
/// 9,
/// 【没做出】
/// </summary>
/// <param name="dals"></param>
private void UnionWhereZip(IList<PersonInfo> dals)
{
IList<PersonInfo> dals2 = new List<PersonInfo>();
dals2.Add(new PersonInfo() { Id = 11, Name = "rain", Age = 23, Sex = "男" });
dals2.Add(new PersonInfo() { Id = 22, Name = "tly", Age = 30, Sex = "女" });
dals2.Add(new PersonInfo() { Id = 3, Name = "zyn", Age = 25, Sex = "女" });
dals2.Add(new PersonInfo() { Id = 4, Name = "yxq", Age = 23, Sex = "女" });
dals2.Add(new PersonInfo() { Id = 5, Name = "yhw", Age = 26, Sex = "男" });
dals.Union(dals2);
Write(dals);
}
/// <summary>
/// 10,排序
/// 【没做出】
/// </summary>
/// <param name="dals"></param>
private void OrderBy(IList<PersonInfo> dals)
{
Write(dals);
Response.Write("<hr>");
dals.OrderBy(p => p.Age);
Write(dals);
}
/// <summary>
/// ylbtech:对泛型集合操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
IList<PersonInfo> dals = new List<PersonInfo>();
dals.Add(new PersonInfo() { Id = 1, Name = "rain", Age = 23, Sex = "男" });
dals.Add(new PersonInfo() { Id = 2, Name = "tly", Age = 30, Sex = "女" });
dals.Add(new PersonInfo() { Id = 3, Name = "zyn", Age = 25, Sex = "女" });
dals.Add(new PersonInfo() { Id = 4, Name = "yxq", Age = 23, Sex = "女" });
dals.Add(new PersonInfo() { Id = 5, Name = "yhw", Age = 26, Sex = "男" });
//输出年龄最大
//1,
//Write(dals);
//2,
//RemoveAt(dals,1);
//Write(dals);
//3,
//Single(2, dals);
//dals.Clear(); //移除所有的项
//Write(dals);
//4,
//Attributes(dals);
//5,
//ToArray(dals);
//6,
//ToDictionary(dals);
//7,
//ToList(dals);
//8,
//ToLookup(dals);
// IEnumerable<PersonInfo> dalIEnum = dals.Where(p => p.Id == 1);
//PersonInfo dal = (PersonInfo)dals.Where(p => p.Id == 1);
//dalIEnum.
Response.End();
}
}