list 求合并相同元素的值

dys_198102 2012-04-19 10:32:38
a 3
b 5
c 9
d 1
b 7

上边中有相同的b,合并后效果
a 3
b 12
c 9
d 1

/*List<DataClass.jx_so> listk = new List<DataClass.jx_so>();

List<string> listL = new List<string>();
List<string> listL2 = new List<string>();
for (int k = 0; k < listk.Count; k++)
{
//if (listL.IndexOf(listk[k]) == -1)
// listL.Add();
}
*/

求高人帮忙解决一下!

...全文
495 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
rayyu1989 2012-04-20
  • 打赏
  • 举报
回复
KeyValuePair<string, int>


里面的int 你可以改成1个自己写的class类嘛 那个只是1个简单的作品
dys_198102 2012-04-20
  • 打赏
  • 举报
回复
楼上老大的方法,我试了一下,但我一条数据有多个参数

我数据格式如下

partno model color qty

0001 本本 黑色 3

0002 笔 红色 6

0001 本本 黑色 2

0003 小本 白色 9


我用您的方法,结果把各各参数给加起来了。
变成了 00010001 本本本本。。。。
rayyu1989 2012-04-20
  • 打赏
  • 举报
回复
用 dictionary吧
插入时候不会重复的


/// <summary>
/// 插入数据
/// </summary>
/// <param name="name"></param>
/// <param name="number"></param>
/// <remarks></remarks>
private void AddData(string name, int number)
{
if (mydata.ContainsKey(name)) {
mydata[name] += number;
} else {
mydata[name] = number;
}
}
/// <summary>
/// 申明字典
/// </summary>
/// <remarks></remarks>
Dictionary<string, int> mydata = new Dictionary<string, int>();
/// <summary>
/// 遍历字典
/// </summary>
/// <remarks></remarks>
private void ForData()
{
foreach (KeyValuePair<string, int> subdata in mydata) {
string a = subdata.Key;
//键值 如你的abcde
int b = subdata.Value;
//数值总数
}
}

dys_198102 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 8 楼 的回复:

我的意思是:
public jx_so(string x, int y,string A,string B,string C) : this()
{
X = x;
Y = y;
A=a;
B=B;
C=c;
}里边的参数多了好多,但还是以……

可以的啊,多写几个let就行了啊
[/Quote]


指点一下,怎么弄。谢谢
rayyu1989 2012-04-20
  • 打赏
  • 举报
回复
直接用dictionary 不是更好

msdn下吧
EnForGrass 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

我的意思是:
public jx_so(string x, int y,string A,string B,string C) : this()
{
X = x;
Y = y;
A=a;
B=B;
C=c;
}里边的参数多了好多,但还是以……
[/Quote]
可以的啊,多写几个let就行了啊
dys_198102 2012-04-20
  • 打赏
  • 举报
回复
初学,楼上的见笑了。。。。很多不懂的地方
三代 2012-04-20
  • 打赏
  • 举报
回复
牛逼啊牛逼
dys_198102 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 的回复:]

引用 3 楼 的回复:
C# code
listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();

小赞一下
[/Quote]
我的new DataClass.jx_so 和我15楼发的Pro差不多,

listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();
所以红色部分会有问题
风之影子 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
C# code
listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();
[/Quote]
小赞一下
dys_198102 2012-04-20
  • 打赏
  • 举报
回复
/// <summary>
/// 声明一个产品类,用来存储多个单子合并后的产品记录
/// </summary>
public class Pro
{
private string model, color, change_type, tax, customer_name, part_no;
private double unitprice;

public Pro(string Model, string Color, string Part_no, string Change_type, string Tax, string Customer_name, double Unitprice)
{
this.model = Model;
this.color = Color;
this.part_no = Part_no;
this.change_type = Change_type;
this.tax = Tax;
this.customer_name = Customer_name;
this.unitprice = Unitprice;
}

public string Model
{
get { return model; }
set { model = value; }
}
public string Color
{
get { return color; }
set { color = value; }
}
public string Change_type
{
get { return change_type; }
set { change_type = value; }
}
public string Tax
{
get { return tax; }
set { tax = value; }
}
public string Customer_name
{
get { return customer_name; }
set { customer_name = value; }
}
public string Part_no
{
get { return part_no; }
set { part_no = value; }
}
}
还是弄的不对。。
cjh200102 2012-04-19
  • 打赏
  • 举报
回复
楼上不错
q107770540 2012-04-19
  • 打赏
  • 举报
回复
listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();
dys_198102 2012-04-19
  • 打赏
  • 举报
回复
哈哈,谢谢,我试试!
threenewbee 2012-04-19
  • 打赏
  • 举报
回复
List<DataClass.jx_so> listk = new List<DataClass.jx_so>()
{
new DataClass.jx_so() { x = "a", y = 3 }, //假设a 3两个值装在x y两个成员里
new DataClass.jx_so() { x = "b", y = 5 },
...
};
var result = from j in listk
group j by j.x into g
select new DataClass.jx_so() { x = g.Key, y = g.Sum(z => z.y) };
dys_198102 2012-04-19
  • 打赏
  • 举报
回复
我的意思是:
public jx_so(string x, int y,string A,string B,string C) : this()
{
X = x;
Y = y;
A=a;
B=B;
C=c;

}里边的参数多了好多,但还是以x为分组,以这个为准合并
EnForGrass 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

if (listk != null)
{
var qwlist = from wp in listk
group wp by wp.Part_no into g
let sum = g.Sum(s => s.Qty)
……
[/Quote]
不就是你写的那样,可能我理解错了

var qwlist = from wp in listL
group wp by wp.x into g
let sum = g.Sum(s => s.y)
select new
{
x = g.Key,
x1 = g.Key,
x2 = g.Key,
ysum = sum
};
foreach (var pq in qwlist)
{
Console.WriteLine(pq.x + " " + pq.x1 + " " + pq.x2 + " " + pq.ysum.ToString());
}
dys_198102 2012-04-19
  • 打赏
  • 举报
回复
if (listk != null)
{
var qwlist = from wp in listk
group wp by wp.Part_no into g
let sum = g.Sum(s => s.Qty)
select new
{
Part_no = g.Key,
Model=g.Key,
Color=g.Key,
ysum = sum
};
foreach (var pq in qwlist)
{
Response.Write(pq.Model+"-"+pq.Color + " " + pq.ysum.ToString());
//Console.WriteLine(pq.x + " " + pq.ysum.ToString());
}
我response.write出的,model,color这些参数都一样,都是part_no,怎么写可以多加参数进去
EnForGrass 2012-04-19
  • 打赏
  • 举报
回复
用let给你写一个
        public class jx_so
{
public string X { get; set; }
public int Y { get; set; }

public jx_so()
{
}
public jx_so(string x, int y) : this()
{
X = x;
Y = y;
}
}
static void Main(string[] args)
{
List<jx_so> listL = new List<jx_so>()
{
new jx_so(){x="a",y=3},
new jx_so(){x="b",y=5},
new jx_so(){x="c",y=9},
new jx_so(){x="d",y=1},
new jx_so(){x="b",y=7},
new jx_so(){x="c",y=4}
};
var qwlist = from wp in listL
group wp by wp.x into g
let sum = g.Sum(s => s.y)
select new
{
x = g.Key,
ysum = sum
};
foreach (var pq in qwlist)
{
Console.WriteLine(pq.x + " " + pq.ysum.ToString());
}
}
/*
a 3
b 12
c 13
d 1
*/

和你的问题一样
参考Linq的Let用法

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧