I have a list in c# as:
List<Data> uData = new List<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"
}
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.
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.
--Updated---
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))
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.
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.
Output,InputandCreatedBy" ? in other wordsuData.GroupBy(l =>new { l.Ouput,l.Input, l.CreatedBy })correct? if this is the case then the result sequence should be empty as there are no objects that happen to have the sameOutput,InputandCreatedByin the data you've shown? I'm I missing something here?var myList1 = uData.GroupBy(x => (x.Input, x.CreatedBy)) .Any(x => x.Count() > 1);oruData.GroupBy(g=> new {g.CreatedBy,g.Input}).Any(x => x.Count() > 1);. if you're still not getting the desired results please consider stating what you want as a result and why? the more information with examples the better.