groupby分组C#之LINQ中常⽤的ToLookup,GroupBy,Join之⽤法总结⼀、ToLookup
Lookup<TKey,TElement> 类
ToLookup⽅法是LINQ常⽤的扩展⽅法,有4个重载版本如下
重载1:ILookup<TKey,TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
重载2:ILookup<TKey,TElement> ToLookup<TSource,TKey>(this IEnumerable<TSource> source,
Func<TSource,TKey> keySelector)
重载3:ILookup<TKey,TElement> ToLookup<TSource,TKey>(this IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, IEqualityComparer<TKey> comparer)
重载4:ILookup<TKey,TElement> ToLookup<TSource,TKey,TElement>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, IEqualityComparer<TKey> comparer)
class Student
{
public string Name;
public double Weight;
public int Age;
}
class Program
{
static void Main(string[] args)
{
LookupExample();
}
public static void LookupExample()
{
List<Student> students = new List<Student> { new Student { Name = "张三", Weight = 55.2, Age = 17 },
new Student { Name = "李三", Weight = 78.7, Age =18 },
new Student { Name = "刘三", Weight = 64.0, Age = 19 },
new Student { Name = "周元", Weight = 59.3, Age = 17 },
new Student { Name = "张⼤", Weight = 73.8, Age = 18 } };
//ToLookup是LINQ常⽤的扩展⽅法,有4个重载版本如下
//重载1:ILookup<TKey,TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func //重载2:ILookup<TKey,TElement> ToLookup<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector)
//重载3:ILookup<TKey,TElement> ToLookup<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, IEqualityCompare //重载4:ILookup<TKey,TElement> ToL
ookup<TSource,TKey,TElement>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<T Lookup<int, string> lookup =(Lookup<int, string>)students.ToLookup(p => p.Age,
p => p.Name + ",体重=" + p.Weight+"kg,年龄="+p.Age+"岁");
//使⽤IGrouping<int, string> 作为循环元素的类型,因为Lookup<TKey,TElement>类实现了IEnumerable<IGrouping<TKey,TElement>>接⼝
foreach (IGrouping<int, string> packageGroup in lookup)
{
Console.WriteLine(packageGroup.Key);
//使⽤string作为循环元素的类型,因为IGrouping<TKey,TElement> 接⼝继承于IEnumerable<TElement>接⼝
foreach (string str in packageGroup)
Console.WriteLine(" {0}", str);
}
int count = lookup.Count;
IEnumerable<string> cgroup = lookup[17];
Console.WriteLine("\n年龄为17岁的学⽣:");
foreach (string str in cgroup)
Console.WriteLine(str);
bool hasG = lookup.Contains(17);
Console.Read();
}
}
⼆、GroupBy
GroupBy的作⽤是对数据进⾏分组,分组后的每个对象元素是继承于IGrouping<TKey,TElement> 接⼝的分组对象。
class Student
{
public string Name;
public double Weight;
public int Age;
public override string ToString()
{
return $"姓名={Name},体重={Weight}Kg,年龄={Age}岁";
}
}
class Program
{
static void Main(string[] args)
{
LookupExample();
}
public static void LookupExample()
{
List<Student> students = new List<Student> { new Student { Name = "张三", Weight = 55.2, Age = 17 },
new Student { Name = "李三", Weight = 78.7, Age =18 },
new Student { Name = "刘三", Weight = 64.0, Age = 19 },
new Student { Name = "周元", Weight = 59.3, Age = 17 },
new Student { Name = "张⼤", Weight = 73.8, Age = 18 } };
IEnumerable<IGrouping<int, Student>> groups = students.GroupBy(g => g.Age);
//以下语句会得到同样的结果
// IEnumerable<IGrouping<int, Student>> groups =from stu in students Group stu By stu.Age;
foreach(IGrouping<int,Student> g in groups)
{
Console.WriteLine("Key=" + g.Key);
//由于IGrouping<TKey,TElement>从IEnumerable<TElement>继承⽽来,所以下⾯foreach中可以使⽤foreach(Student stu in g)
foreach (Student stu in g)
{
Console.WriteLine(stu.ToString());
}
}
Console.Read();
}
}
GroupBy也存在多个重载,其中常⽤的⼀个重载如下:
public static IEnumerable<IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
使⽤该重载,则上述例⼦可以改为如下表⽰:
IEnumerable<IGrouping<int, string>> groups = students.GroupBy(r => r.Age, r => r.Name);
foreach(IGrouping<int,string> g in groups)
{
Console.WriteLine("Key=" + g.Key);
//由于IGrouping<TKey,TElement>从IEnumerable<TElement>继承⽽来,所以下⾯foreach中可以使⽤foreach(string stuStr in g) foreach (string stuStr in g)
{
Console.WriteLine(stuStr);
}
}
三、Join
Join⽅法同样是位于Enumerable类中,是实现IEnumerable<T>接⼝的所有实现类的扩展⽅法,具有两个重载:
重载1:public static IEnumerable<TResult> Join<TOuter,TInner,TKey,TResult> (this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector);
重载2:public static IEnumerable<TResult> Join<TOuter,TInner,TKey,TResult> (this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector, IEqualityComparer<TKey> comparer);
类型参数
TOuter
第⼀个序列中的元素的类型。
TInner
第⼆个序列中的元素的类型。
TKey
键选择器函数返回的键的类型。
TResult
结果元素的类型。
参数
outer
<TOuter>
要联接的第⼀个序列。
inner
<TInner>
要与第⼀个序列联接的序列。
outerKeySelector
<TOuter,TKey>
⽤于从第⼀个序列的每个元素提取联接键的函数。
innerKeySelector
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论