62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CSDNReply
{
class Program
{
static void Main()
{
int[] ids = new int[] { 12, 16, 16 };
string[] serials = new string[] { "2011061330301", "2011061330301", "2011061324084" };
string[] names = new string[] { "Sennheiser PXC 450", "Sennheiser PX 100-II Black", "Sennheiser PX 100-II Black" };
int[] shippings = new int[] { 1, 1, 1 };
// 将上述一一对应的数组合并成一张Product的表
List<Product> products = new List<Product>();
for (int i = 0; i < ids.Length; i++)
products.Add(new Product(ids[i], serials[i], names[i], shippings[i]));
// 根据产品型号分组
var result = products.GroupBy(p => p._id);
foreach (var group in result)
{
Console.WriteLine("产品编号[{0}]:<{1}>已出货:{2}件,明细如下:",
group.Key,
group.Where(o => o._id == group.Key).FirstOrDefault(),
group.Sum(o => o._shipping));
Console.WriteLine("".PadLeft(40, '-'));
foreach (Product item in group)
Console.WriteLine("\t产品批号<{0}>-出货{1}件", item._serial, item._shipping);
Console.WriteLine();
}
}
}
class Product
{
public int _id = -1;
public string _serial = string.Empty;
public string _name = string.Empty;
public int _shipping = -1;
public Product(int id, string serial, string name, int shipping)
{
this._id = id;
this._serial = serial;
this._name = name;
this._shipping = shipping;
}
}
}
产品编号[12]:<CSDNReply.Product>已出货:1件,明细如下:
----------------------------------------
产品批号<2011061330301>-出货1件
产品编号[16]:<CSDNReply.Product>已出货:2件,明细如下:
----------------------------------------
产品批号<2011061330301>-出货1件
产品批号<2011061324084>-出货1件
int[] ids = new int[]
{
12,
16,
16
};
string[] serials = new string[]
{
"2011061330301",
"2011061330301",
"2011061324084"
};
string[] names = new string[]
{
"Sennheiser PXC 450",
"Sennheiser PX 100-II Black",
"Sennheiser PX 100-II Black"
};
int[] shippings = new int[]
{
1,
1,
1
};
// 将上述一一对应的数组合并成一张Product的表
List<Product> products = new List<Product>();
for (int i = 0; i < ids.Length; i++)
products.Add(new Product(ids[i], serials[i], names[i], shippings[i]));
var result = products.GroupBy(p => p._id).SelectMany
(g => g.Select(r => new
{
id = g.Key,
serial = r._serial,
name = r._name,
shipping = g.Count()
}));