0

我有这样的数据:

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

不知道如何完成这项任务。

顺便说一句,在 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>();
4

5 回答 5

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();
于 2012-08-29T16:58:13.100 回答
0

您可以利用 LINQ 的GroupBy 扩展。一旦你检索到你的记录(假设你的类是 Record),你就可以通过 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 ) );
}
于 2012-08-29T16:32:31.320 回答
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)
于 2012-08-29T16:33:11.243 回答
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() ;
  }
}
于 2012-08-29T16:51:41.710 回答
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
于 2012-08-29T17:11:29.640 回答