public class Program
{
public static void Main()
{
List<student> stulist = GetList();
如果有多个grand 是一样的,直接出错
var dic1 = stulist.ToDictionary(m => m.Grand);
foreach (var item in dic1)
{
Console.WriteLine("年级:" + item.Key);
Console.WriteLine("\t\t" + item.Value.StuNo + " " + item.Value.Sex);
}
var dicde = stulist.ToLookup(m => m.Grand);
foreach (var item in dicde)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1.StuNo + " " + item1.Sex);
}
}
var dicaab = stulist.ToLookup(m => m.Grand, p => p.StuNo + " -- " + p.Sex);
foreach (var item in dicaab)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1);
}
}
var dicaa = stulist.ToLookup(i => i.Grand, j => { return j.StuNo + "--" + j.Sex; });// stulist.ToDictionary(m => m.Grand, p => p.StuNo + " -- " + p.Sex);
foreach (var item in dicaa)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1);
}
}
Dictionary<string, List<string>> dic = stulist.ToDictionarys(p => p.Grand, p => p.StuNo);
foreach (var item in dic)
{
Console.WriteLine(item.Key );
foreach (var s in item.Value)
{
Console.WriteLine("\t\t" + "---" + s);
}
}
Dictionary<string, List<student>> dicca = stulist.ToDictionarys(p => p.Grand, p => p);
foreach (var item in dicca)
{
Console.WriteLine(item.Key);
foreach (var s in item.Value)
{
Console.WriteLine("\t\t" + "---" + s.StuNo + "--" + s.Sex);
}
}
Console.Read();
}
public static List<student> GetList()
{
return new List<student>()
{
new student(){StuNo="0001",Grand="一年级",Sex="男"},
new student(){StuNo="0002",Grand="二年级",Sex="男"},
new student(){StuNo="0003",Grand="一年级",Sex="女"},
};
}
}
public class student
{
public string StuNo { get; set; } //学号
public string Grand { get; set; } //年级
public string Sex { get; set; } //性别
}
public static class test
{
// http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,d322938ca3a61348
internal class IdentityFunction<TElement>
{
public static Func<TElement, TElement> Instance
{
get { return x => x; }
}
}
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return ToDictionary<TSource, TKey, TSource>(source, keySelector, IdentityFunction<TSource>.Instance);
}
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
Dictionary<TKey, TElement> dic = new Dictionary<TKey, TElement>();
foreach (TSource element in source)
{
if (dic.ContainsKey(keySelector(element)))
{
continue;
}
dic.Add(keySelector(element), elementSelector(element));
}
return dic;
}
public static Dictionary<TKey, List<TElement>> ToDictionarys<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
Dictionary<TKey, List<TElement>> dic = new Dictionary<TKey, List<TElement>>();
foreach (TSource element in source)
{
if (dic.ContainsKey(keySelector(element)))
{
dic[keySelector(element)].Add(elementSelector(element));
continue;
}
dic.Add(keySelector(element), new List<TElement> {elementSelector(element)});
}
return dic;
}
}
{
public static void Main()
{
List<student> stulist = GetList();
如果有多个grand 是一样的,直接出错
var dic1 = stulist.ToDictionary(m => m.Grand);
foreach (var item in dic1)
{
Console.WriteLine("年级:" + item.Key);
Console.WriteLine("\t\t" + item.Value.StuNo + " " + item.Value.Sex);
}
var dicde = stulist.ToLookup(m => m.Grand);
foreach (var item in dicde)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1.StuNo + " " + item1.Sex);
}
}
var dicaab = stulist.ToLookup(m => m.Grand, p => p.StuNo + " -- " + p.Sex);
foreach (var item in dicaab)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1);
}
}
var dicaa = stulist.ToLookup(i => i.Grand, j => { return j.StuNo + "--" + j.Sex; });// stulist.ToDictionary(m => m.Grand, p => p.StuNo + " -- " + p.Sex);
foreach (var item in dicaa)
{
Console.WriteLine("年级:" + item.Key);
foreach (var item1 in item)
{
Console.WriteLine("\t\t" + item1);
}
}
Dictionary<string, List<string>> dic = stulist.ToDictionarys(p => p.Grand, p => p.StuNo);
foreach (var item in dic)
{
Console.WriteLine(item.Key );
foreach (var s in item.Value)
{
Console.WriteLine("\t\t" + "---" + s);
}
}
Dictionary<string, List<student>> dicca = stulist.ToDictionarys(p => p.Grand, p => p);
foreach (var item in dicca)
{
Console.WriteLine(item.Key);
foreach (var s in item.Value)
{
Console.WriteLine("\t\t" + "---" + s.StuNo + "--" + s.Sex);
}
}
Console.Read();
}
public static List<student> GetList()
{
return new List<student>()
{
new student(){StuNo="0001",Grand="一年级",Sex="男"},
new student(){StuNo="0002",Grand="二年级",Sex="男"},
new student(){StuNo="0003",Grand="一年级",Sex="女"},
};
}
}
public class student
{
public string StuNo { get; set; } //学号
public string Grand { get; set; } //年级
public string Sex { get; set; } //性别
}
public static class test
{
// http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,d322938ca3a61348
internal class IdentityFunction<TElement>
{
public static Func<TElement, TElement> Instance
{
get { return x => x; }
}
}
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return ToDictionary<TSource, TKey, TSource>(source, keySelector, IdentityFunction<TSource>.Instance);
}
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
Dictionary<TKey, TElement> dic = new Dictionary<TKey, TElement>();
foreach (TSource element in source)
{
if (dic.ContainsKey(keySelector(element)))
{
continue;
}
dic.Add(keySelector(element), elementSelector(element));
}
return dic;
}
public static Dictionary<TKey, List<TElement>> ToDictionarys<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
Dictionary<TKey, List<TElement>> dic = new Dictionary<TKey, List<TElement>>();
foreach (TSource element in source)
{
if (dic.ContainsKey(keySelector(element)))
{
dic[keySelector(element)].Add(elementSelector(element));
continue;
}
dic.Add(keySelector(element), new List<TElement> {elementSelector(element)});
}
return dic;
}
}