2012-02-15 68 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 
相关问题