2012-02-15 50 views
0

我有例如("1:2","5:90","7:12",1:70,"29:60")其中ID和數量是由分開的陣列「:」(冒號),我想要做的是,當有ID的重複的程序將增加數量,並返回一組新的在示例中它將變成(「1:72」,「5:90」,「7:12」,「29:60」)。如何檢查數組中的重複項,然後使用它們的值執行某些操作?

例2("1:2","5:90","7:12","1:70","29:60","1:5")變爲("1:77","5:90","7:12","29:60")。

我想解決它不使用LINQ。

回答

1

如果你想它沒有LINQ ...

var totalQuantities = new Dictionary<int, int>(); 
foreach(var raw in sourceArr) { 
    var splitted = raw.Split(':'); 
    int id = int.Parse(splitted[0]); 
    int qty = int.Parse(splitted[1]); 
    if(!totalQuantities.ContainsKey(id)) { 
     totalQuantities[id] = 0; 
    } 
    totalQuantities[id] += qty; 
} 

var result = new string[totalQuantities.Count]; 
int i=0; 
foreach(var kvp in totalQuantities) { 
    result[i] = string.Format("{0}:{1}", kvp.Key, kvp.Value); 
    i++; 
} 
2

你必須手動完成。遍歷每個列表,檢查每個元素的ID。把它放在Dictionary<int, int>,Dictionary<id, qt>。如果字典包含該id,則將其添加到該值。

  1. 循環,添加,檢查用字典類。
1
(
    from raw in arr 
     let splitted = raw.Split(':') 
     let id = int.Parse(splitted[0]) 
     let qty = int.Parse(splitted[1]) 
     let data = new { id, qty } 
     group data by data.id into grp 
      let totalQty = grp.Sum(val => val.qty) 
      let newStr = string.Format("{0}:{1}", grp.Key, totalQty 
      select newStr 
) 
.ToArray() 

注意,代碼可能包含意外的錯誤,因爲它是寫在記事本。

+0

先生,謝謝你++提供的答案,我會嘗試一個,但你可以給一個不使用linq的例子。我想看看 – 2012-02-15 18:12:09

+0

這是怎麼回事?我不承認讓,但它看起來像linq。 – Stuart 2012-02-15 18:12:12

+0

是的,'let'是LINQ。 – jason 2012-02-15 18:14:00

1
var input=new string[]{"1:2","5:90","7:12","1:70","29:60","1:5"}; 
var result=input 
      .Select(s=>s.Split(':')) 
      .Select(x=>x.Select(s=>int.Parse(s)).ToArray()) 
      .GroupBy(x=>x[0]) 
      .Select(g=>g.Key+":"+g.Sum(x=>x[1])); 

我懶得指定文化無處不在。在投入生產之前,您可能想要這樣做,否則它會因具有不尋常整數表示的文化而失敗。

var totals=new Dictionary<int,int> 
foreach(string s in input) 
{ 
    string[] parts=s.Split(':'); 
    int id=int.Parse(parts[0]); 
    int quantity=int.Parse(parts[0]); 
    int totalQuantity; 
    if(!totals.TryGetValue(id,out totalQuantity)) 
     totalQuantity=0;//Yes I know this is redundant 
    totalQuanity+=quantity; 
    totals[id]=totalQuantity; 
} 
var result=new List<string>(); 
foreach(var pair in totals) 
{ 
    result.Add(pair.Key+":"+pair.Value); 
} 
+0

LINQ的+1! :) – 2012-02-15 18:09:52

+0

先生,謝謝++提供的答案,我會嘗試一個,但你可以給一個不使用LINQ的例子。我想看看如何去那個 – 2012-02-15 18:12:15

+0

@ randelramirez1增加了一個非LINQ版本,但它更加醜陋。 – CodesInChaos 2012-02-15 18:23:11

3
var foo = array.Select(s => s.Split(':')) 
       .GroupBy(x => x[0]) 
       .Select(g => 
        String.Format(
         "{0}:{1}", 
         g.Key, 
         g.Sum(x => Int32.Parse(x[1])) 
        ) 
       ) 
       .ToArray(); 

注意,這是沒有必要解析 「鍵,」 只值。

沒有LINQ:

var dictionary = new Dictionary<string, int>(); 
foreach (var group in array) { 
    var fields = group.Split(':'); 
    if (!dictionary.ContainsKey(fields[0])) { 
     dictionary.Add(fields[0], 0); 
    } 
    dictionary[fields[0]] += Int32.Parse(fields[1]); 
} 
string[] foo = new string[dictionary.Count]; 
int index = 0; 
foreach (var kvp in dictionary) { 
    foo[index++] = String.Format("{0}:{1}", kvp.Key, kvp.Value); 
} 
+1

因爲你忽略TryGetValue'的'輸出,只需用'ContainsKey' – CodesInChaos 2012-02-15 18:22:10

+0

@CodeInChaos:謝謝。 – jason 2012-02-15 18:35:56

1

試試這個:

List<string> items = new List<string>(new string[] { "1:2", "5:90", "7:12", "1:70", "29:60" }); 
Dictionary<string, int> dictionary = new Dictionary<string, int>(); 

foreach (string item in items) 
{ 
    string[] data = item.Split(':'); 
    string key = data[0]; 

    if (!dictionary.ContainsKey(data[0])) 
    { 
     int value = dictionary[data[0]]; 
     dictionary[key] += int.Parse(data[1]); 
    } 
} 

//Used dictionary values here 
相關問題