C#Linq查找多个分组依据的重复项 [英] C# Linq Find duplicates with multiple group by
问题描述
我在c#中有一个列表为:
I have a list in c# as:
List<Data> uData = new List<uData>();
从UI填充uData的位置:
Where uData is populated from UI as:
{
Id: 1,
Name: "Smith",
Input: "7,8",
Output: "Output1",
CreatedBy: "swallac",
CreatedON: "12/01/2018"
},
{
Id: 2,
Name: "Austin",
Input: "9,10",
Output: "Output1",
CreatedBy: "amanda",
CreatedON: "12/03/2018"
},
{
Id: 3,
Name: "Smith",
Input: "22,22",
Output: "Output2",
CreatedBy: "swallac",
CreatedON: "12/01/2018"
},
{
Id: 4,
Name: "Smith",
Input: "9,8",
Output: "Output2",
CreatedBy: "aaa",
CreatedON: "12/01/2018"
},
{
Id: 5,
Name: "Peter",
Input: "7,8",
Output: "Output3",
CreatedBy: "swallac",
CreatedON: "12/02/2018"
}
我想做的是在输出"键上搜索此列表,并找出输入"和输入"的对应组合值中是否存在重复项. CreatedBy密钥.
What I want to do is search this list on "Output" key, and find out if there are in duplicates in the corresponding combination value of "Input" & CreatedBy key.
例如,在上面的示例列表中,我有三个输出:Output1,Output2,Output3.现在对于将输出值的键设置为"Output1"&的列表"Output3"对应的"Input"& "CreatedBy"键值在此处重复.值为"7,8"&. "swallac"作为组合值.这就是我要强调的
For example, in my above example list I have three Output: Output1,Output2, Output3. Now for lists with key of Output value as "Output1" & "Output3" the corresponding "Input" & "CreatedBy" key value is duplicate here. The value being "7,8"& "swallac" as combined value. This is what I want to highlight
为此,我尝试了以下查询:
For this I tried out the below query:
var myList = uData.GroupBy(l => l.Ouput)
.SelectMany(g => g.GroupBy(x => (x.Input, x.CreatedBy)).Where(x => x.Count() > 1))
.SelectMany(x => x);
这不会给我任何错误,但由于列出了所有数据,所以没有给我想要的结果.我在这里想念什么.
This does not gives me any error but does not gives me desired result as it lists all the data. What am I missing here.
-更新----
之前,我希望不要在一个输出中重复输入,因为我有以下查询.
Earlier I wanted that the Input should not be repeated in one Output because of which I had the below query.
uData.GroupBy(l => l.Ouput)
.Any(g => g.GroupBy(x => x.Input).Any(x => x.Count() > 1))
现在,我要另一个查询来检查列表中是否重复了Input和CreatedBy的组合.
Now I want another query to check if the combination of Input and CreatedBy is repeated in the list.
我根据建议尝试了上面发布的查询和下面的查询:
I tried the above posted query and below query as per the suggestion:
uData.GroupBy(g=> new {g.CreatedBy,g.Input})
.Where(w=>w.Count() > 1)
但这会返回所有列表,而不是重复的列表
But this returns me all the list instead of the duplicate
已更新以添加示例链接:
Updated to add an example link:
https://dotnetfiddle.net/HWMYp6
我已经在上面的链接中创建了示例.
I have created the example in above link.
在该示例中,我想将ID为10的集合与输出(output5)标记为重复,因为Input和by创建的这种组合在ID 1,2,3中早已存在(所有这些都属于output1).因此,基本上输入和createby的一种组合不应在另一组上重复.参考键为输出. 抱歉,如果我的初始帖子不太清楚.我尝试过.
In the example I want to mark the set with id 10 with output (output5) as the duplicate as such combination of Input and created by already existed before in id 1,2,3 (all of which belong to output1). So basically one combination of input and createby should not be repeated over another set. The reference key being Output. Sorry if my initial post was not very clear. I tried.
推荐答案
似乎您只想按创建者"和输入"进行分组,在这种情况下,对当前查询进行少量修改就足够了:>
it seems like you want to group by the "created by" and "input" only in which case a slight modification to your current query should suffice:
var result = uData.GroupBy(x => (x.Input, x.CreatedBy))
.Where(x => x.Count() > 1)
.SelectMany(x => x);
我只是删除了Output
字段的GroupBy
.
-
GroupBy
读为按输入和创建dBy分组" -
Where
的意思是保留有两个或多个项目的组" -
SelectMany
将嵌套序列折叠为IEnumerable<Data>
GroupBy
reads as "group by Input and CreatedBy"Where
reads as "retain the groups where there are two or more items"SelectMany
collapse the nested sequences into aIEnumerable<Data>
更新:
鉴于您的修改,您正在寻找:
Given your edit, you're looking for:
var myList = uData.GroupBy(x => new {x.Input, x.CreatedBy})
.SelectMany(x => x.GroupBy(z => z.Output).Skip(1))
.SelectMany(x => x);
这篇关于C#Linq查找多个分组依据的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!