簡體   English   中英

如何從Excel范圍中刪除重復項? C#

[英]How do I remove duplicates from excel range? c#

我已經在我的excel范圍內將單元格從字符串轉換為字符串列表,並在原始列表中的逗號后分隔了每個項目。 我開始認為我實際上並沒有分離每個項目,它們仍然是一個整體,試圖弄清楚如何正確地做到這一點,以便每個項目(即the_red_bucket_01)都是自己的字符串。

單元格1和2中原始字符串的示例:

單元格1:

the_red_bucket_01, the_blue_duck_01,_the green_banana_02, the orange_bear_01

單元格2:

the_purple_chair_01, the_blue_coyote_01,_the green_banana_02, the orange_bear_01

新清單看起來像這樣,盡管我不確定它們是單獨的項目:

the_red_bucket_01
the_blue_duck_01
the green_banana_02
the orange_bear_01
the_red_chair_01
the_blue_coyote_01
the green_banana_02
the orange_bear_01

現在,我想刪除重復項,以便控制台僅顯示每一項,無論它們有多少,我似乎都無法使我的foreah / if語句正常工作。 我假設它正在打印項目的多個副本,因為它正在對列表中的每個項目進行迭代,因此它返回的數據就是很多項目。

 foreach (Excel.Range item in xlRng)
                    {
                        string itemString = (string)item.Text;


                        List<String> fn = new List<String>(itemString.Split(','));


                        List<string> newList = new List<string>();

                        foreach (string s in fn)
                        if (!newList.Contains(s))
                        {
                            newList.Add(s);
                        }
                        foreach (string combo in newList)
                        {
                            Console.Write(combo);
                        }

如果您在閱讀時擔心不同的項目,則只需使用像fn.Distinct()這樣的Distinct運算符fn.Distinct()

為了處理整個數據,我可以建議兩種方法:

  1. 讀取整個數據,然后使用LINQ的Distinct運算符

  2. 或者使用Set數據結構並在讀取Excel的同時存儲其中的每個元素

如果您正在處理數據,我建議您看一下LINQ文檔。 它具有非常好的擴展。 有關更多方法,您可以簽出MoreLINQ軟件包。

您可能需要修剪字符串,因為它們具有前導空格,因此“ string1”與“ string1”不同。

foreach (string s in fn)
if (!newList.Contains(s.Trim()))
{
     newList.Add(s);
}

您可以使用Distinct使用Linq簡化此操作。

通過使用默認的相等比較器比較值,從序列中返回不同的元素。

foreach (Excel.Range item in xlRng)
{
    string itemString = (string)item.Text;

    List<String> fn = new List<String>(itemString.Split(','));
    foreach (string combo in fn.Distinct())
    {
         Console.Write(combo);
    }
}

如另一個答案中所述,您可能還需要Trim任何空格,在這種情況下,您可以這樣做:

fn.Select(x => x.Trim()).Distinct()

在需要包含鍵/值的地方,最好使用Dictionary類型。 嘗試將List<T>代碼更改為Dictionary<T> 即來自:

List<string> newList = new List<string>();

foreach (string s in fn)
if (!newList.Containss))
{
     newList.Add(s);
}

Dictionary<string, string> newList = new Dictionary<string, string>();

foreach (string s in fn)
if (!newList.ContainsKey(s))
{
     newList.Add(s, s);
}

我認為如果將newList移出循環,您的代碼可能會按預期工作-在每個循環中創建一個名為newList的新變量,這樣就不會從較早的循環中找到重復項。

您可以使用Linq更簡潔地完成所有這些操作:

//set up some similar data
string list1 = "a,b,c,d,a,f";
string list2 = "a,b,c,d,a,f";
List<string> lists = new List<string> {list1,list2};

// find unique items
var result = lists.SelectMany(i=>i.Split(',')).Distinct().ToList();

SelectMany()將列表列表“拉平”到列表中。

Distinct()刪除重復項。

var uniqueItems = new HashSet<string>();
foreach (Excel.Range cell in xlRng)
{
    var cellText = (string)cell.Text;

    foreach (var item in cellText.Split(',').Select(s => s.Trim()))
    {
        uniqueItems.Add(item);
    }
}

foreach (var item in uniqueItems)
{
    Console.WriteLine(item);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM