C# 多列列表<;T>;-基于另一列查找一列的重复项

C# 多列列表<;T>;-基于另一列查找一列的重复项,c#,linq,lambda,duplicates,duplicate-data,C#,Linq,Lambda,Duplicates,Duplicate Data,我需要根据另一列(优先级)查找一列(数量)的重复项。我有一个包含以下数据的列表: Priority Product Qty 0 a 10 0 b 20 1 c 50 1 d 20 1 e 50 1 f 10 1 g 20 1 h 10 我

我需要根据另一列(
优先级
)查找一列(
数量
)的重复项。我有一个包含以下数据的列表:

Priority  Product  Qty
0           a       10
0           b       20
1           c       50
1           d       20
1           e       50
1           f       10
1           g       20
1           h       10
我需要生成一个排序的
列表
,其中只包含优先级
0
的项目中的
数量
重复项

即,生成的
列表将包含:

Priority  Product  Qty
0           a       10
1           f       10
1           h       10
0           b       20
1           d       20
1           g       20

是否有一个简单的LINQ/Lambda表达式用于执行此操作?

假设您有一个名为list的可枚举容器,其中包含具有属性Priority、Product和Qty的元素:

var orderedResult = list.Where(element => !list.Contains(x => x != element && x.Priority == element.Priority && x.Qty == element.Qty)).OrderBy(element => element.Qty).ThenBy(element => element.Priority);
试试这个:

static void Main(string[] args)
{
    var items = new List<Item>
    {
        new Item { Priority = 0, Product = "a", Qty = 10 },
        new Item { Priority = 0, Product = "b", Qty = 20 },
        new Item { Priority = 1, Product = "c", Qty = 50 },
        new Item { Priority = 1, Product = "d", Qty = 20 },
        new Item { Priority = 1, Product = "e", Qty = 50 },
        new Item { Priority = 1, Product = "f", Qty = 10 },
        new Item { Priority = 1, Product = "g", Qty = 20 },
        new Item { Priority = 1, Product = "h", Qty = 10 }
    };

    foreach (var group in items.Where  (i => i.Priority == 0)
                               .GroupBy(i => i, g => items
                               .Where  (t => t.Qty == g.Qty && 
                                             t.Product != g.Product)))
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(group.Key);                   // Priority == 0
        Console.ForegroundColor = ConsoleColor.Gray;
        foreach (var item in group.SelectMany(i => i))  // dups
            Console.WriteLine("\t{0}", item);
    }
}

class Item
{
    public int    Priority { get; set; }
    public string Product  { get; set; }
    public int    Qty      { get; set; }
    public override string ToString()
    {
        return String.Format("{0}\t{1}\t{2}",
                             this.Priority, this.Product, this.Qty);
    }
}
static void Main(字符串[]args)
{
var items=新列表
{
新项目{优先级=0,产品=“a”,数量=10},
新项目{优先级=0,产品=“b”,数量=20},
新项目{优先级=1,Product=“c”,数量=50},
新项目{Priority=1,Product=“d”,数量=20},
新项目{Priority=1,Product=“e”,数量=50},
新项目{Priority=1,Product=“f”,Qty=10},
新项目{Priority=1,Product=“g”,Qty=20},
新项目{优先级=1,产品=“h”,数量=10}
};
foreach(items.Where中的var组(i=>i.Priority==0)
.GroupBy(i=>i,g=>items
其中(t=>t.数量==g.数量和
t、 产品!=g.Product)
{
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(group.Key);//优先级==0
Console.ForegroundColor=ConsoleColor.Gray;
foreach(组中的变量项。SelectMany(i=>i))//dups
Console.WriteLine(“\t{0}”,项);
}
}
类项目
{
公共int优先级{get;set;}
公共字符串乘积{get;set;}
公共整数数量{get;set;}
公共重写字符串ToString()
{
返回String.Format(“{0}\t{1}\t{2}”,
此优先级、此产品、此数量);
}
}
以下是一个解决方案:


谢谢,但这不会返回一个包含结果的列表,所以我觉得测试有点困难。返回结果列表需要添加什么?谢谢@BartoszKP。这似乎很好地解决了问题。你知道我为什么在我的问题上得到-1吗?@BartoszKP你是如何在文本中列出这个列表的?我尝试了很久,然后放弃了,并张贴了没有它的问题???抱歉这么傻,但你现在应该已经猜到我是新来的!!!使用反撇号。你也可以点击你的问题下的“编辑”,看看我是怎么做的。您可以找到更多信息。@BartoszKP得到了它,并在编辑和您提供的链接中找到了它。没有人如此盲目。再次感谢你的帮助。
var result = input
    .GroupBy(p => p.Qty)
    .Where(g => g.Any(p0 => p0.Priority == 0))
    .Where(g => g.Skip(1).Any())
    .SelectMany(g => g)
    .OrderBy(g => g.Qty);