Q:

Find items with duplicate key but different values

I have a list that contains multiple items. Each item consists of three vlaues. ID, Name, Value. I need to get only items where ID and Name are equal but have different Value.
public class MyModel
{
    public int ElementID { get; set; }
    public string ElementName { get; set; }
    public string ElementValue { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {

        var list = new List<MyModel>();

        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name1", ElementValue = "Id1Name1Value1"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name1", ElementValue = "Id1Name1Value2"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name2", ElementValue = "Id1Name2Value1"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name3", ElementValue = "Id1Name3Value1"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name3", ElementValue = "Id1Name3Value2"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name4", ElementValue = "Id1Name4Value1"});
        list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name4", ElementValue = "Id1Name4Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name1", ElementValue = "Id2Name1Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name1", ElementValue = "Id2Name1Value2"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name2", ElementValue = "Id2Name2Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name3", ElementValue = "Id2Name3Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name3", ElementValue = "Id2Name3Value2"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name4", ElementValue = "Id2Name4Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value1"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value2"});
        list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value3"});

        //Expected output
        //Id1Name1Value1
        //Id1Name1Value2
        //Id1Name3Value1
        //Id1Name3Value2
        //Id2Name1Value1
        //Id2Name1Value2
        //Id2Name3Value1
        //Id2Name3Value2
        //Id2Name5Value1
        //Id2Name5Value2
        //Id2Name5Value3
    }

}

I'm not getting the lambda expression right. I can group it, but I can't figure out the best way to filter out only the items where ElementValue are different within the group ElementID and ELementName.
I guess that the solution is really simple, but I'm stuck...
All help is appreciated.
Thanks to 'vc 74', I got it to work using your code!.
var foo = list.GroupBy(i => 
    new
    {
        i.ElementID,
        i.ElementName
    }).
    Where(g => g.Select(i => i.ElementValue).Distinct().Count() > 1);

 foreach (var item in foo)
            {
                Console.WriteLine(item.Key.ElementName);

                foreach (var myModel in list.Where(x => x.ElementID == item.Key.ElementID && x.ElementName == item.Key.ElementName).ToList())
                {
                    Console.WriteLine($"  {myModel.ElementValue}");
                }

            }

            Console.ReadLine();

It was to obvious and the Distinct on ElementValue on each group did the trick. I will create an extension method that will return a list with items that have duplicates but different values. I'm thinking about a generic version where you could define what properties on an object that should act as a group and what property to use as value. Maybe you can use Attributes on the properties to define if they are to be used in the group or as a value. We'll see...

A:

To select the id/name combinations with multiple values:
list.GroupBy(i => 
    new
    {
        i.ElementID,
        i.ElementName
    }).
    Where(g => g.Select(i => i.ElementValue).Distinct().Count() > 1);

Note that this returns the groups containing items you're looking for, each with its items.
Fiddle

