1

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: "7,8",
   Output: "Output1",
   CreatedBy: "awanda",
   CreatedON: "12/03/2018"
},
{
   Id: 1,
   Name: "Smith",
   Input: "22,22",
   Output: "Output2",
   CreatedBy: "swallac",
   CreatedON: "12/01/2018"
},
{
   Id: 1,
   Name: "Smith",
   Input: "9,8",
   Output: "Output2",
   CreatedBy: "swallac",
   CreatedON: "12/01/2018"
},
{
   Id: 1,
   Name: "Peter",
   Input: "1,10",
   Output: "Output3",
   CreatedBy: "pjon",
   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 value of "Input" 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" the corresponding "Input" key is duplicate here. The value being "7,8". This is what I want to highlight. So Output1 has duplicate Input which Output2 & Output3 have not.

I can do like below to first find out output then check for the value:

var myList = uData.Where(p => p.Output == "Output").First();

But I wont be knowing all the Output in advance to check.

Any inputs to get started on this?

1
  • To group your items by Output values, you can use ToLookup Commented Dec 12, 2018 at 15:03

6 Answers 6

1

You can get a list of the duplicated items as follows:

var myList = uData.GroupBy(l => l.Ouput)
                  .SelectMany(g => g.GroupBy(x => x.Input).Where(x => x.Count() > 1))
                  .SelectMany(x => x);
  • group by the output to get a sequence of related items by Output
  • use SelectMany to flatten the result of grouping by the input where there is a duplicate
  • use SelectMany to flatten to a single IEnumerable<Data>

Otherwise, if you just want to know whether there are duplicates based on the said criteria then you can use Any

var anyDuplicates = uData.GroupBy(l => l.Ouput)
                         .Any(g => g.GroupBy(x => x.Input).Any(x => x.Count() > 1));
Sign up to request clarification or add additional context in comments.

4 Comments

@karen you're welcome, wish I could explain a bit more but I am not in a position to do so. only had 5-10 mins break and thought I'd give it a go.
Bothering you again here but if I need to find duplicates based on two keys like Input & CreatdBy based on same Output what do I need to do. I tried with the below code. var myList = uData.GroupBy(l => l.Ouput) .SelectMany(g => g.GroupBy(x => (x.Input, x.CreatedBy)).Where(x => x.Count() > 1)) .SelectMany(x => x); But this does not gives me desired result. Let me know if I should created a new ques for this.
@karen what you've stated should work, are you sure that in your data there are objects that happen to have the same CreatdBy, Input and Ouput? if you're confident you're doing it right and still not getting results then it might be wise to post a new question.
I have created a new post here: stackoverflow.com/questions/53764417/… if you can have a look.
1

If I understand your question correctly, you don't want to search for a specific output, but rather find all duplicate Output and Input values. You can do that by grouping on the combination and filtering the groups with more than one entry:

var duplicates = uData.GroupBy(d=>new{d.Input,d.Output}).Where(g=>g.Count() > 1)

For your example, the above returns (an enumerable of) a group with as its key {Output: "Output1", Input: "7,8}. (The group itself contains all the elements with that combination)

Comments

0

Try to use GroupBy

var myList = uData.Where(p => p.Output == "Output").GroupBy(l => l.Input);

So you can get this list that is sorting by the input and loop through each. Save the first input, and compare it to the second element. Do as you wish if they match, or update your variable with the next input and compare until they don't match again.

3 Comments

that'll do it nicely and keeps it maintainable MSDN has a nice example
@Jay I wont be knowing the value "Output" so cant use the code above. I want to first someone keep all the similar lists based on output key together and then make comparison on the Input key of these.
change it to be uData.GroupBy(l => l.Input);, then once you know the "input" you can check it against the query using .contains
0

Use Linq:

   var dups = uData.GroupBy(g => g).Where(group => group.Count() > 1).Select(group => 
   group.Key).Select(out => out.Output == "Output").ToList();

Comments

0

You can use a lookup, which will create an object that you can iterate using the key you specify :

public class Program
{
    public static void Main(string[] args)
    {
        List<data> myList = new List<data>();
        myList.Add(new data("output1", "1,5"));
        myList.Add(new data("output2", "1,6"));
        myList.Add(new data("output1", "1,5"));
        myList.Add(new data("output1", "2,0"));

        ILookup<string, string> myLookup = myList.ToLookup(d => d.output, d => d.input);

        foreach (IGrouping<string, string> items in myLookup)
        {
            Console.WriteLine("Output : " + items.Key);
            foreach (string input in items)
            {
                Console.WriteLine("-- Input value is " + input);
            }
        }
    }
}

public class data
{
    public string output;
    public string input;

    public data(string output, string input)
    {
        this.output = output;
        this.input = input;
    }
}

Output will look like :

Output : output1
-- Input value is 1,5
-- Input value is 1,5
-- Input value is 2,0
Output : output2
-- Input value is 1,6

Then you can check if any two values of input are the same.

Comments

0

You can group by Output and select those groups whose Input Dictionary have count less than the count of the group.

 uData.GroupBy(g => g.Output)
     .Where(group => group.Value
                          .Select(x => x.Input)
                          .ToDictionary(y => y).Count() != 
            group.Value.Count())
     .Select(duplicateGroups => new { Output = duplicateGroups.Key });

This query is not tested but to give you the idea for solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.