日期:2014-05-20 浏览次数:20812 次
s = @"a00001,2 a00001,-1 b00001,1 b00001,1 "; var query = from x in s.Split(new char[] { '\r', '\n' }) .Select(x => new { id = x.split(',')[0], value = Convert.ToInt32(x.split(',')[1]) }) group x by x.id into g select new { id = g.Key, value = g.Select(x => x.value).Sum() }; foreach (var item in query) { Console.WriteLine(item.id + "," + item.value); }
------解决方案--------------------
public static List<String[]> ReadTxt(string filePathName) { List<String[]> ls = new List<String[]>(); StreamReader fileReader=new StreamReader(filePathName); string strLine=""; while (strLine != null) { strLine = fileReader.ReadLine(); if (strLine != null && strLine.Length>0) { ls.Add(strLine.Split(',')); //Debug.WriteLine(strLine); } } fileReader.Close(); return ls; }
------解决方案--------------------
string s = @"a00001,2
a00001,-1
b00001,1
b00001,1";
var query = from x in s.Split(new string[] { "\r\n" }, StringSplitOptions.None)
.Select(x => new { id = x.Split(',')[0].Trim(), value = Convert.ToInt32(x.Split(',')[1]) })
group x by x.id into g
select new { id = g.Key, value = g.Select(x => x.value).Sum() };
foreach (var item in query)
{
Console.WriteLine(item.id + "," + item.value);
}
------解决方案--------------------