我怎么写linq查询 [英] How I can write linq query
本文介绍了我怎么写linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
公共类A
{
public int ID { get ; set ;}
public list< b> lstB { get ; set ;}
}
公共类B
{
Public int ID { get ; st;}
公共字符串名称{ get ; st;}
公共字符串路径{获取; st;}
}
如何编写linq查询以从A类列表中查找重复的名称
什么我试过了:
lstDocumentsWithError = lstA.GroupBy(x => x.lstB).SelectMany(g => ; g.Skip(1))。ToList();
解决方案
视情况而定...
我在下面的列表中测试了以下问题:
列表< A> lstA = new 列表< A>
{
new A(){
ID = 1 , lstB = new 列表< b>()
{
new B() {ID = 1 ,Name = A,Path = C},
new B(){ID = 2 ,Name = A,Path = D},
new B(){ID = 3 ,Name = B,Path = E},
new B(){ID = 4 ,Name = C,Path = F},
new B() {ID = 5 ,名称= D,Path = G}
}},
new A(){
ID = 2 ,lstB = new 列表< b>()
{
new B(){ID = 6 ,Name = D,Path = C},
new B(){ ID = 7 ,Name = E,Path = D},
new B(){ID = 8 ,Name = E,Path = E},
new B(){ID = 9 ,Name = F,Path = F},
new B(){ID = 10 ,Name = F,Path = G}
}},
new A (){
ID = 3 ,lstB = new 列表< b> ()
{
new B(){ID = 11 ,Name = F,Path = < span class =code-string> C},
new B(){ID = 12 ,Name = G,Path = D},
new B(){ ID = 13 ,Name = H,Path = E},
new B(){ID = 14 ,Name = H,Path = F },
new B(){ID = 15 ,Name = H,Path = G}
}},
};
如果你愿意喜欢在整个<上找到类B
(A
类被忽略)的重复的名称 code> lstA ,试试这个:
var lstDocumentsWithError = lstA
.SelectMany(x => x.lstB)
.GroupBy(x = > x.Name)
。其中(grp => grp.Count()> 1)
。选择(grp = > new {Name = grp.Key,Count = grp.Count()})
.ToList();
结果:
名称计数
A 2
D 2
E 2
F 3
H 3
但是如果你想只在类A
中比较名称,你必须将上面的代码更改为:
< pre lang =c#> var lstDocumentsWithError1 = lstA
.SelectMany(x = >
x.lstB.GroupBy(y => y.Name)
.Where(grp => grp.Count()> 1)
。选择(z = > new
{
AID = x.ID,
Name = z .Key,
Count = z.Count()
})
);
结果:
AID名称数量
1 A 2
2 E 2
2 F 2
3 H 3
Public Class A
{
public int ID{get;set;}
public list<b> lstB {get;set;}
}
Public Class B
{
Public int ID{get;st;}
Public string Name{get;st;}
Public string Path{get;st;}
}
how i can write linq query to find duplicate Names from List of Class A
What I have tried:
lstDocumentsWithError = lstA.GroupBy(x => x.lstB).SelectMany(g => g.Skip(1)).ToList();
解决方案
Depending on situation...
I've tested below queries on that list:
List<A> lstA = new List<A> { new A(){ ID = 1, lstB = new List<b>() { new B(){ID = 1, Name="A", Path="C"}, new B(){ID = 2, Name="A", Path="D"}, new B(){ID = 3, Name="B", Path="E"}, new B(){ID = 4, Name="C", Path="F"}, new B(){ID = 5, Name="D", Path="G"} }}, new A(){ ID = 2, lstB = new List<b>() { new B(){ID = 6, Name="D", Path="C"}, new B(){ID = 7, Name="E", Path="D"}, new B(){ID = 8, Name="E", Path="E"}, new B(){ID = 9, Name="F", Path="F"}, new B(){ID = 10, Name="F", Path="G"} }}, new A(){ ID = 3, lstB = new List<b>() { new B(){ID = 11, Name="F", Path="C"}, new B(){ID = 12, Name="G", Path="D"}, new B(){ID = 13, Name="H", Path="E"}, new B(){ID = 14, Name="H", Path="F"}, new B(){ID = 15, Name="H", Path="G"} }}, };
If you would like to find duplicated names of classB
(A
class is ignored) on entirelstA
, try this:
var lstDocumentsWithError = lstA .SelectMany(x=>x.lstB) .GroupBy(x => x.Name) .Where(grp=>grp.Count()>1) .Select(grp=> new {Name = grp.Key, Count = grp.Count()}) .ToList();
Result:
Name Count A 2 D 2 E 2 F 3 H 3
But if you would like to compare names only inside classA
, you have to change above code to:
var lstDocumentsWithError1 = lstA .SelectMany(x => x.lstB.GroupBy(y=>y.Name) .Where(grp=>grp.Count()>1) .Select(z=> new { AID = x.ID, Name = z.Key, Count = z.Count() }) );
Result:
AID Name Count 1 A 2 2 E 2 2 F 2 3 H 3
这篇关于我怎么写linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文