C# 在linq中使用GroupBy时,如何获取组名列表?
如果原始集合中有属性为prop的对象:C# 在linq中使用GroupBy时,如何获取组名列表?,c#,linq,c#-4.0,lambda,C#,Linq,C# 4.0,Lambda,如果原始集合中有属性为prop的对象: prop = "a"; prop = "a"; prop = "b"; prop = "b"; prop = "c"; 我按道具分组,我需要输出: List<string>{ "a", "b", "c" } 列表{“a”、“b”、“c”} 使用group by只能通过比较功能提供单个项目。如果比较是通过prop完成的,那么它将只返回带有distinctprop的对象。您所需要做的就是迭代它们并只选择prop例如 public class F
prop = "a";
prop = "a";
prop = "b";
prop = "b";
prop = "c";
我按道具分组,我需要输出:
List<string>{ "a", "b", "c" }
列表{“a”、“b”、“c”}
使用group by只能通过比较功能提供单个项目。如果比较是通过prop
完成的,那么它将只返回带有distinctprop
的对象。您所需要做的就是迭代它们并只选择prop
例如
public class Foo
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
将以下代码添加到组:
var foos = new List<Foo>();
var groupings = from foo in foos
group foo by foo.PropertyA
into groupedFoos
select groupedFoos;
/*
// the same as
var groupings = foos.GroupBy(foo => foo.PropertyA);
*/
var keys = from grouping in groupings
select grouping.Key;
更新
我最初单独使用Disticnt()的答案是不够的。您需要按属性值分组,然后选择每个子集的第一个成员:
myList.GroupBy(i => i.prop).Select(i => i.First()).ToList().ForEach(i => Console.Write(i.prop + ", "));
演示代码
下面是一些说明分组的代码
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var myList = new List<Foo>() {
new Foo(){ prop = "a", anotherProp = "z"},
new Foo(){ prop = "a", anotherProp = "x"},
new Foo(){ prop = "b", anotherProp = "x"},
new Foo(){ prop = "b", anotherProp = "y"},
new Foo(){ prop = "c", anotherProp = "z"}
};
// Display groups.
myList.GroupBy(i => i.prop).ToList().ForEach(j =>
{
Console.WriteLine("\t");
j.ToList().ForEach(k => Console.Write(k.prop + ", "));
});
Console.WriteLine();
Console.WriteLine(new string('-', 25));
// Display desired output.
myList.GroupBy(i => i.prop).Select(i => i.First()).ToList().ForEach(i => Console.Write(i.prop + ", "));
Console.WriteLine();
}
}
public class Foo
{
public string prop { get; set; }
public string anotherProp { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
var myList=new List(){
new Foo(){prop=“a”,anotherProp=“z”},
new Foo(){prop=“a”,anotherProp=“x”},
new Foo(){prop=“b”,anotherProp=“x”},
new Foo(){prop=“b”,anotherProp=“y”},
新建Foo(){prop=“c”,anotherProp=“z”}
};
//显示组。
myList.GroupBy(i=>i.prop).ToList().ForEach(j=>
{
控制台写入线(“\t”);
j、 ToList().ForEach(k=>Console.Write(k.prop+“,”);
});
Console.WriteLine();
Console.WriteLine(新字符串('-',25));
//显示所需的输出。
GroupBy(i=>i.prop).Select(i=>i.First()).ToList().ForEach(i=>Console.Write(i.prop+“,”);
Console.WriteLine();
}
}
公开课Foo
{
公共字符串属性{get;set;}
公共字符串另一个属性{get;set;}
}
}
列出组上下文.YourObjects.Select(o=>o.prop.Distinct().ToList();
如果您愿意:
stuff.GroupBy(e => e.prop).Select(group => group.Key)
List source=getSource();
列表组=源
.GroupBy(x=>x.prop)
.ToList();
列表键=组
.选择(g=>g.Key)
.ToList();
我这样做了:foos.GroupBy(f=>f.PropertyA)。根据您的答案选择(gr=>gr.Key)。非常感谢。您不需要选择每个分组的第一项。如果您使用组的属性键
来检索所需的值,这将更加优雅!
List<strings> groups context.YourObjects.Select(o => o.prop).Distinct().ToList();
stuff.GroupBy(e => e.prop).Select(group => group.Key)
var q = from a in yourList
group a by a.prop into b
select b.Key;
List<MyClass> source = getSource();
List<IGrouping<KeyType, MyClass>> groups = source
.GroupBy(x => x.prop)
.ToList();
List<KeyType> keys = groups
.Select(g => g.Key)
.ToList();