在C#列表中重复计算特定值的次数

我有一个7列的列表,其中有几行数据是使用Oledb从Excel表导入的。 现在我想通过列表,并从第1列检查第一列的第一行的值,并找出多less次它出现在列表中,如果它不止一次出现,我想存储所有其他列值以及另一个列表中的第一列进行一些操作

例如,我在列表中有以下几行数据

Time CC Month prc str tx qq 14:46:00 NP 14-Dec 4.0000 6,000 14:46:00 LN 14-Dec 6.00 Put 2.0090 1,500 14:46:00 LN 14-Dec 6.00 Call 0.0095 1,500 14:42:57 LN 14-Sep 3.85 Put 0.0860 50 14:42:57 LN 14-Sep 3.85 Call 0.1200 50 14:34:00 LN 14-Sep 3.25 Put 0.0025 200 14:34:00 LN 14-Sep 3.50 Put 0.0100 100 14:32:00 NP 14-Dec 4.0000 2,000 14:32:00 LN 14-Dec 6.00 Put 2.0090 500 14:32:00 LN 14-Dec 6.00 Call 0.0095 500 14:27:00 LN 15-Mar 4.50 Call 0.2230 100 14:26:00 LN 15-Mar 4.50 Call 0.2210 200 

在列表中的上述数据行中,我有第一列作为timetime列第一行有14:46:00 ,现在要检查的次数是14:46:00被重复并插入所有的行和列与14:46:00相关联, 14:46:00它们执行一些算术运算,并转移到下一个唯一的值为14:42:57的时间,然后重复该过程

Time 14:46:00示例List1将如下所示

 14:46:00 NP 14-Dec 4.0000 6,000 14:46:00 LN 14-Dec 6.00 Put 2.0090 1,500 14:46:00 LN 14-Dec 6.00 Call 0.0095 1,500 

一旦在上面的列表中完成了一些操作,它可以被time 14:42:57下一组值覆盖

我已经导入数据从Excel工作表中列出使用下面的代码行

 var fileName = string.Format("{0}\\AxiomParser.xlsx", Directory.GetCurrentDirectory()); var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 12.0;", fileName); var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ", connectionString); var ds = new DataSet(); adapter.Fill(ds, "axiomData"); DataTable data = ds.Tables["axiomData"]; List<DataRow> listAxiomData = data.AsEnumerable().ToList(); List<AxiomDS> resultAxiomData = new List<AxiomDS>(); foreach (var row in listAxiomData) { var entries = new AxiomDS(); entries.time = Convert.ToDateTime(row[0]); entries.CC = row[1].ToString(); entries.term = row[2].ToString(); if (row[3].ToString() != "") { entries.strike = Convert.ToDouble(row[3]); } entries.strategy = row[4].ToString(); if (row[5].ToString() != "") { entries.price = Convert.ToDouble(row[5]); } entries.quantity = Convert.ToInt32(row[6]); resultAxiomData.Add(entries); } 

 public class AxiomDS { public DateTime time { get; set; } public string CC { get; set; } public string term { get; set; } } public class Program { static void Main(string[] args) { List<AxiomDS> resultAxiomData = new List<AxiomDS>(); var uniqueTimes = resultAxiomData.Select(a => a.time).Distinct(); foreach (var uniqueTime in uniqueTimes) { // Find all records that have this time var recordsToProcess = resultAxiomData.Where(r => r.time == uniqueTime); // TODO: foreach (var record in recordsToProcess) { // Do something with this list } } } } 

这听起来像是你想按time对你的条目进行分组,然后对每个有多条logging的组进行一些工作。 LINQ使这很简单:

 var groupedData = from result in resultAxiomData group result by result.time into resultGroup where resultGroup.Count() > 1 order by resultGroup.Key select resultGroup; 

现在你可以遍历分组结果了:

 foreach (var timeGroup in groupedData) { // timeGroup.Key is the time foreach (var entry in timeGroup) { // process the entry } } 

使用LINQ到对象:

 public class Row { DateTime Time {get; set;} string CC { get; set; } string Month {get; set;} double PRC {get; set;} string Str {get; set;} double TX {get; set;} int QQ { get; set; } } var allEntries = new List<Row>(); //Load data... var allEntriesDict = allEntries.Select(entry => entry.Time) .Distinct() .ToDictionary(time => time, time => allEntries.Where(entry => entry.Time == time).ToList()); foreach(var kvp in allEntriesDictionary) { //kvp.Key is the Time //kvp.Value is a List<Row> DoCalculations(kvp.Value); }