我有这样的数据:
Name Update
CC 12/31/2012
CS 12/31/2012
CI 04/30/2013
CR 04/30/2013
我需要这个输出:
Annual CC/CS Update: 12/31/2012
Annual CI/CR Update: 04/30/2013
如果我的数据是这样的话,那么:
Name Update
CC 12/31/2012
CI 04/30/2013
我需要这个输出:
Annual CC Update: 12/31/2012
Annual CI Update: 04/30/2013
不确定如何完成此任务。
BTW,在ASP.Net MVC3中使用Razor
尝试这样做:(注意AllianceDesignations定义为Designation [])
<div class="span6">
@AllianceDesignations.GroupBy(x => x.UpdateDue).Select(g => String.Format("<p>Annual {0} Update: {1}</p>", String.Join("/", g.Select(y => y.Name).ToArray()), g.Key))
</div>
显示:
<div class="span6">
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Linq.IGrouping`2[System.DateTime,MVC3MyPage.MyPageService.Designation],System.String]
</div>
另外,我已经把它们放在一个字典中用于另一种用途:
Dictionary<string, Designation> Designations = new Dictionary<string, Designation>();
答案 0 :(得分:1)
var designationsByDate = allianceDesignations.GroupBy(designation => designation.UpdateDue).OrderBy(values => values.Key);
string[] rowsOfData = designationsByDate.Select(designationByDate => {
string annual = designationByDate.Aggregate(string.Empty, (acc, designation) => acc + "/" + designation.Name);
return string.Format("Annual {0} Update {1}", annual, designationByDate.Key);
}).ToArray();
答案 1 :(得分:0)
您可以利用LINQ的GroupBy extension。一旦你检索了你的记录(让我们假设你的类是记录),你可以查询你的结果LINQ扩展。代码可能不完美但它应该给你正确的想法。
IEnumerable<Record> records = ....
var groups = records.GroupBy( f => f.Update );
foreach( IEnumerable<Record> r in groups ){
Write( String.Join( "/", r.Select( rec => rec.Name ).ToArray() ), r.First().Update ) );
}
答案 2 :(得分:0)
首先group by Update
,然后String.Join
每个组中的名称(未经测试):
data.GroupBy(x => x.Update)
.Select(g => String.Format("Annual {0} Update: {1}", String.Join("/", g.Select(y => y.Name).ToArray()), g.Key)
答案 3 :(得分:0)
如果您的数据源是SQL,您可以尝试这样的查询:
select Update , Name
from my_table
order by Update , Name
现在您的数据已正确订购。您可以阅读它并将其分组,如果没有linq,就像这样:
using ( SqlConnection conn = new SqlConnection( "some connect string" ) )
using ( SqlCommand cmd = conn.CreateCommand() )
{
cmd.CommandText = @"select Update , Name from my_table order by Update , Name" ;
cmd.CommandType = CommandType.Text ;
using ( SqlDataReader reader = cmd.ExecuteReader() )
{
DateTime prev_update = DateTime.MinValue ;
List<string> names = new List() ;
while ( reader.Read() )
{
DateTime update = reader.GetDateTime( 0 ) ;
String name = reader.GetString( 1 ) ;
if ( update != prev_update )
{
if ( name.Length > 0 )
{
Console.WriteLine( "Annual {0} Update: {1}" , string.Join( "/" , names ) , prev_update ) ;
}
names.Clear() ;
prev_update = update ;
}
names.Add( name ) ;
}
reader.Close() ;
}
}
答案 4 :(得分:0)
不确定您是否正在读取某个文件,并且对确切的格式有点不清楚,但是这里有一些代码可以完全按照您指定的格式从纯文本文件中读取所需的代码。
//used to store converted data
Dictionary<string, string> data = new Dictionary<string, string>();
//open file, read line by line and store values as needed
using (TextReader tr = File.OpenText("test.txt"))
{
//throw away header line
string line = tr.ReadLine();
while ((line = tr.ReadLine()) != null)
{
string[] values = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
if (data.ContainsKey(values[1]))
data[values[1]] += "/" + values[0];
else
data.Add(values[1], values[0]);
}
}
//print results
foreach (string date in data.Keys)
Console.WriteLine(String.Format("Annual {0} Update: {1}", data[date], date));
输入:
Name Update
CS 12/31/12
CI 12/31/12
CL 01/16/13
CS 01/16/13
输出:
Annual CS/CI Update: 12/31/12
Annual CL/CS Update: 01/16/13