string[,] a={{"d","10"},
{"b","10"},
{"b","10"},
{"a","10"},
{"c","10"}
}希望得出如下数组string[,] b={
{"a","10"},
{"b","20"},
{"c","10"},
{"d","10"},
}感谢您的指导!!!

解决方案 »

  1.   

    创建一个字典
    Dictionary<TKey, TValue>,
    TKey就是你用来排序的那个属性
    TValue就是后面的数字然后对Dictionary的TKey集合进行排序就ok了
      

  2.   


    void Main()
    {
     
      
    List<test> list=new List<test>();
    list.Add(new test("d",10));
    list.Add(new test("b",10));
    list.Add(new test("b",10));
    list.Add(new test("a",10));
    list.Add(new test("c",10));

    var query= from l in list
               group l by l.key into m
       orderby m.Key 
       select new 
    {
      m.Key,
       sum=m.Sum(n=>n.value)
    };

    query.ToList().ForEach( q=>Console.WriteLine(q.Key +" " +q.sum));
    /*
    a 10
    b 20
    c 10
    d 10
    */
    }public class test
    {
       public string key;
       public int value;
       public test(string k,int v)
    {
      key=k;
      value=v;
    }
    }
      

  3.   

    就事论事,给个答案,g的评论一针见血
    string[,] a ={{"d","10"},
    {"b","10"},
    {"b","10"},
    {"a","10"},
    {"c","10"}
    };
                Dictionary<string, int> dictResult = new Dictionary<string, int>();
                int index=0;
                for (; index < a.Length; index += 2)
                {
                    string key = a[index/2,0];
                    string value = a[index / 2, 1];                int iValue;
                    if (int.TryParse(value, out iValue))
                    {
                        int count;
                        if (dictResult.TryGetValue(key, out count))
                        {
                            dictResult[key] = count + iValue;
                        }
                        else
                        {
                            dictResult[key] = iValue;
                        }
                    }
                }            string[,] b = new string[dictResult.Count, 2];
                index=0;
                foreach (var kp in dictResult)
                {
                    b[index / 2, 0] = kp.Key;
                    b[index / 2, 1] = kp.Value.ToString();
                    index += 2;
                }
      

  4.   


    string[,] a =
    {
    {"d","10"},
    {"b","10"},
    {"b","10"},
    {"a","10"},
    {"c","10"}
    };var q = Enumerable.Range(0, 5).Select(i =>
    Enumerable.Range(0, 2).Select(j => a[i, j]).ToArray());
    q = q.GroupBy(r => r[0]).Select(g =>
    new string[] { g.Key, g.Sum(r => Convert.ToInt32(r[1])).ToString() });string[,] b = new string[q.Count(), 2];
    q.Select((r, i) => r.Select((n, j) => b[i, j] = n).ToArray()).ToArray();