您的位置:首页 > 编程语言 > C#

C#查找数组中相同的项并合并

2012-10-25 20:39 453 查看
CSDN 问题帖:查找数组中相同的项并合并.C#这个真心没SQL方便.哈哈.可能不懂LINQ吧.求指导.

http://topic.csdn.net/u/20121024/20/e395374b-176e-4f4e-899e-e360e6aa72df.html?87994

按SQL中的办法.传一个ID.写个函数合并值.用C#实现如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SumString
{
class Program
{
static string[,] str = new string[,] { { "1", "aaa" }, { "1", "bbb" }, { "2", "ccc" }, { "2", "ddd" },
{ "3", "uuu" }, { "1", "eee" }, { "4", "aaa" }, { "4", "qqq" } };
static List<string[,]> list = new List<string[,]>();
static List<string> listid = new List<string>();

static void Main(string[] args)
{
Addlist();
foreach (string[,] str in list)
{
if (!listid.Contains(str[0, 0]))
{
if(SumString(str).TrimEnd(new char[]{','}).Contains(","))
Console.WriteLine("{0}--{1}", str[0, 0], SumString(str).TrimEnd(new char[]{','}));
}

listid.Add(str[0,0]);

}
Console.ReadKey();

}
public static string SumString(string [,] para)
{
string str="";
foreach (string[,] s in list)
{
if (s[0, 0] == para[0,0])
{
str += s[0, 1] + ",";
}
}
return str;
}
public static void Addlist( )
{
string[,] tempstr = new string[1, 2];
for (int i = 0; i < str.GetLength(0); i++)
{
tempstr = new string[1, 2];
for (int j = 0; j < str.GetLength(1); j++)
{
if(j==0)
tempstr[0, 0] = str[i, j];
else
tempstr[0, 1] = str[i, j];

}
list.Add(tempstr);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: