C# 此表达式在GroupBy方法中的含义是什么?
通过这个Lambda,它总是返回true,我认为它的意思是“将占位符x设为true(因为字符串本身总是true??),并按集合中的任何true进行分组” 我的问题是为什么我在GroupBy中放置一个字符串总是返回true 我不太确定,当我在集合中发现重复值时,我受到了启发,以下是示例:C# 此表达式在GroupBy方法中的含义是什么?,c#,linq,C#,Linq,通过这个Lambda,它总是返回true,我认为它的意思是“将占位符x设为true(因为字符串本身总是true??),并按集合中的任何true进行分组” 我的问题是为什么我在GroupBy中放置一个字符串总是返回true 我不太确定,当我在集合中发现重复值时,我受到了启发,以下是示例: bool test = anyCollection.GroupBy(x => "AnyKeyString").Any(g => g.Count() > 1); 公共类字母表 { 公共int I
bool test = anyCollection.GroupBy(x => "AnyKeyString").Any(g => g.Count() > 1);
公共类字母表
{
公共int ID{get;set;}
公共字符串初始值{get;set;}
}
List testList=新列表()
{
新字母表(){ID=1,Initial=“A”},
新字母表(){ID=2,Initial=“B”},
新字母表(){ID=3,Initial=“C”},
新字母表(){ID=4,Initial=“D”},
};
List testList2=新列表()
{
新字母表(){ID=1,Initial=“A”},
新字母表(){ID=2,Initial=“A”},
新字母表(){ID=3,Initial=“C”},
新字母表(){ID=4,Initial=“C”},
};
booltest1=testList.GroupBy(x=>x.Initial).Any(g=>g.Count()>1);
//假的
booltest2=testList2.GroupBy(x=>x.Initial).Any(g=>g.Count()>1);
//真的
booltest3=testList2.GroupBy(x=>“Initial”).Any(g=>g.Count()>1);
//真的
有点离题了,我怎样才能根据泛型类型列表进行分组
public class Alphabet
{
public int ID { get; set; }
public string Initial { get; set; }
}
List<Alphabet> testList = new List<Alphabet>()
{
new Alphabet() { ID = 1, Initial = "A"},
new Alphabet() { ID = 2, Initial = "B"},
new Alphabet() { ID = 3, Initial = "C"},
new Alphabet() { ID = 4, Initial = "D"},
};
List<Alphabet> testList2 = new List<Alphabet>()
{
new Alphabet() { ID = 1, Initial = "A"},
new Alphabet() { ID = 2, Initial = "A"},
new Alphabet() { ID = 3, Initial = "C"},
new Alphabet() { ID = 4, Initial = "C"},
};
bool test1 = testList.GroupBy(x => x.Initial).Any(g => g.Count() > 1);
// false
bool test2 = testList2.GroupBy(x => x.Initial).Any(g => g.Count() > 1);
// true
bool test3 = testList2.GroupBy(x => "Initial").Any(g => g.Count() > 1);
// true
List testList=newlist(){
“A”,
“B”,
“C”,
“D”
};
List testList2=新列表(){
“A”,
“A”,
“C”,
“D”
};
var k=testList.GroupBy(x=>?).Any(g=>g.Count()>1);
var c=testList2.GroupBy(x=>A”).Any(g=>g.Count()>1);
//永远正确
.GroupBy
不是过滤器-它不是.Where
.GroupBy
接受一个键选择器:Func
因此,如果您有这样一个函数(相当于x=>x.Initial
):
它可以作为:.GroupBy(GetGroupKeyForObject)
然后,它将按初始值分组。但是如果您有这样一个函数(相当于x=>“anystring”
):
然后,为了分组的目的,所有项目都将被确定为具有键“anystring”
如果只想选择首字母为“anystring”的项目,则应执行以下操作:
public string GetGroupKeyForObject(Alphabet alpha)
{
return "anystring";
}
或者更高效(但可读性较差)的方式:
假设您有这个测试集:
字母表(为简洁起见仅显示首字母,但与您的字母相同):[“A”、“A”、“B”、“B”、“B”、“C”、“D”、“E”、“F”]
按首字母分组,将得到6组:
关键字:分组项目
答:[“A”,“A”]
B:[“B”、“B”、“B”、“B”]
C:[“C”]
D:[“D”]
E:[“E”]
F:[“F”]
但如果按“anystring”分组,则会得到1组:
关键字:分组项目
任意字符串:[“A”、“A”、“B”、“B”、“B”、“C”、“D”、“E”、“F”]
因此,对第一个示例执行.Any(x=>x.Count()>1)
将返回true
,因为A和B的计数大于1。
对第二个示例执行相同的操作与对原始集合调用
.Count()>1
相同,因为您通过一个静态值对整个选择进行分组,因此只能得到一个组。。GroupBy
不是过滤器-它不是。Where
.GroupBy
接受一个键选择器:Func
因此,如果您有这样一个函数(相当于x=>x.Initial
):
它可以作为:.GroupBy(GetGroupKeyForObject)
然后,它将按初始值分组。但是如果您有这样一个函数(相当于x=>“anystring”
):
然后,为了分组的目的,所有项目都将被确定为具有键“anystring”
如果只想选择首字母为“anystring”的项目,则应执行以下操作:
public string GetGroupKeyForObject(Alphabet alpha)
{
return "anystring";
}
或者更高效(但可读性较差)的方式:
假设您有这个测试集:
字母表(为简洁起见仅显示首字母,但与您的字母相同):[“A”、“A”、“B”、“B”、“B”、“C”、“D”、“E”、“F”]
按首字母分组,将得到6组:
关键字:分组项目
答:[“A”,“A”]
B:[“B”、“B”、“B”、“B”]
C:[“C”]
D:[“D”]
E:[“E”]
F:[“F”]
但如果按“anystring”分组,则会得到1组:
关键字:分组项目
任意字符串:[“A”、“A”、“B”、“B”、“B”、“C”、“D”、“E”、“F”]
因此,对第一个示例执行.Any(x=>x.Count()>1)
将返回true
,因为A和B的计数大于1。
对第二个示例执行相同的操作与对原始集合调用
.Count()>1
相同,因为您使用一个静态值对整个选择进行分组,因此只得到一个组。x=>“anystring”
不是谓词-它不是true或false,而是为分组选择键testList2.GroupBy(x=>anystring”).Any(g=>g.Count()>1)
等同于testList2.Count()>1
那么它指向什么呢?至少不存在这样的密钥,那么为什么它会返回真值呢?哦,这是你的问题吗.GroupBy()
不是.Where()
。它不是一个过滤器。@john不,这是我的问题,它在寻找重复的记录,如果你去掉了.Any(),它的意思仍然是一样的:),那么你的问题到底是什么?在别人回答了你的问题后,你不能完全改变你的问题。你问为什么.GroupBy(“anystring”)
“返回true”,我已经回答了,并给出了完整的解释。现在,您已将问题更改为提出一个完全不同的问题。x=>“anystrin
public string GetGroupKeyForObject(Alphabet alpha)
{
return "anystring";
}
bool result = testList.Where(a => a.Initial == "anystring").Count() > 1;
bool result = testList.Where(a => a.Initial == "anystring").Skip(1).Any();