2012-02-15 9 views
0

私は配列(例えば、"1:2","5:90","7:12",1:70,"29:60")を持っています。IDとQtyは ':'(コロン)で区切られています。IDの重複がある場合、プログラムはqtyを追加して新しいセットを返します。 (例: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を使用しない例を挙げることができます。私はどのようにそれが行くでしょう –

+0

が表示されますか?私はletを認識しませんが、linqのように見えます。 – Stuart

+0

はい、 'let'はLINQです。 – jason

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

+1 LINQ! :) –

+0

先生、ありがとうございました+私は答えを提供するために私はその1つを試みますが、linqを使用しない例を与えることができます。私はどのように行くだろうかを確認したい –

+0

@ randelramirez1非linqのバージョンを追加しましたが、それは非常に醜いです。 – CodesInChaos

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

+0

@CodeInChaos:ありがとうございます。 – jason

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 
関連する問題