110,534
社区成员
发帖
与我相关
我的任务
分享
/// <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;
//数值总数
}
}
listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();
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) };
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());
}
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
*/