c# - 删除重复商品并返回订单号

标签 c# linq

我想删除列表中的重复项目。我可以轻松地通过 Distinct() 实现它。但是我还需要获取已删除项目的订单号。我在 linq 中找不到任何函数解决问题,最终实现为以下代码:

    public List<string> Repeat(List<string> str)
    {
        var Dlist = str.Distinct();
        List<string> repeat = new List<string>();
        foreach (string aa in Dlist)
        {
            int num = 0;
            string re = "";
            for (int i = 1; i <= str.LongCount(); i++)
            {
                if (aa == str[i - 1])
                {
                    num = num + 1;
                    re = re + " - " + i;
                }
            }
            if (num > 1)
            {
                repeat.Add(re.Substring(3));
            }
        }
        return repeat;
    }

还有其他更简单的方法来解决这个问题吗?或者我错过了 linq 中的任何功能吗?任何建议将不胜感激。

最佳答案

如果我没记错的话,这个查询的作用与你的函数完全相同:

var repeated = str.GroupBy(s => s).Where(group => group.Any())
    .Select(group =>
    {
        var indices = Enumerable.Range(1, str.Count).Where(i => str[i-1] == group.Key).ToList();
        return string.Join(" - ", group.Select((s, i) => indices[i]));
    });

它首先对原始列表的项目进行分组,使得具有相同内容的每个项目都在一个组中。然后它在原始列表中搜索该组中项目的所有索引,这样我们就有了该组原始项目的所有索引。然后它将索引连接到一个字符串,以便生成的格式与您请求的格式类似。您还可以将此语句 lambda 转换为匿名 lambda:

var repeated = str.GroupBy(s => s).Where(group => group.Any())
    .Select(group => string.Join(" - ",
        group.Select((s, i) =>
            Enumerable.Range(1, str.Count).Where(i2 => str[i2 - 1] == group.Key).ToList()[i])));

但是,这会显着降低性能。


我使用以下代码对此进行了测试:

public static void Main()
{
    var str = new List<string>
    {
        "bla",
        "bla",
        "baum",
        "baum",
        "nudel",
        "baum",
    };
    var copy = new List<string>(str);
    var repeated = str.GroupBy(s => s).Where(group => group.Any())
        .Select(group => string.Join(" - ",
            group.Select((s, i) =>
                Enumerable.Range(1, str.Count).Where(i2 => str[i2 - 1] == group.Key).ToList()[i])));
    var repeated2 = Repeat(str);
    var repeated3 = str.GroupBy(s => s).Where(group => group.Any())
        .Select(group =>
        {
            var indices = Enumerable.Range(1, str.Count).Where(i => str[i-1] == group.Key).ToList();
            return string.Join(" - ", group.Select((s, i) => indices[i]));
        });

    Console.WriteLine(string.Join("\n", repeated) + "\n");
    Console.WriteLine(string.Join("\n", repeated2) + "\n");
    Console.WriteLine(string.Join("\n", repeated3));
    Console.ReadLine();
}

public static List<string> Repeat(List<string> str)
{
    var distinctItems = str.Distinct();
    var repeat = new List<string>();
    foreach (var item in distinctItems)
    {
        var added = false;
        var reItem = "";
        for (var index = 0; index < str.LongCount(); index++)
        {
            if (item != str[index]) 
                continue;
            added = true;
            reItem += " - " + (index + 1);
        }

        if (added)
            repeat.Add(reItem.Substring(3));
    }

    return repeat;
}

其输出如下:

1 - 2
3 - 4 - 6
5

1 - 2
3 - 4 - 6
5

1 - 2
3 - 4 - 6
5

关于c# - 删除重复商品并返回订单号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48634839/

相关文章:

Linq To Sql Multiple Where 在 Foreach 循环中搜索

c# - Entity Framework 代码优先 CTP5 : How to define non primitive types

c# - 表达 LINQ .Single() 语句是显式的 NUnit 测试

c# - 如果您有一个使用 Select() 方法的 LINQ 语句,是否有办法从下一条记录中获取值?

c# - Case 语句中的 Linq 表达式

c# - where if 语句在 linq 查询 c#

c# - 什么是将 IDictionary<string, object> 转换为 IDictionary<string, string> 的优雅方法

c# - 类型初始化异常

c# - Linqpad 6 (Core) 和.Net Core Api?

c# - Silverlight 真的不支持 OpenGL 吗?