C# 使用c从数组列表中获取数据#
我无法从数组列表中获取一些数据 名单是 日期、id、出价、时间、谈话、stC# 使用c从数组列表中获取数据#,c#,C#,我无法从数组列表中获取一些数据 名单是 日期、id、出价、时间、谈话、st 2017-07-26 23:53:14,15a4ca7e,13,274,265,OK 2017-07-26 23:56:11,14f418a8,29,131,108,OK 2017-07-26 23:55:59,14df675e,31,189,168,OK 2017-07-26 23:56:54,1557ff5c,30,155,155,OK 2017-07-26 23:55:56,158db0b1,04,221,219,
2017-07-26 23:53:14,15a4ca7e,13,274,265,OK
2017-07-26 23:56:11,14f418a8,29,131,108,OK
2017-07-26 23:55:59,14df675e,31,189,168,OK
2017-07-26 23:56:54,1557ff5c,30,155,155,OK
2017-07-26 23:55:56,158db0b1,04,221,219,OK
2017-07-26 23:57:48,155a9e93,39,118,110,OK
2017-07-26 23:59:08,15aa6a2c,30,44,23,OK
2017-07-27 00:00:16,1596cd53,66,0,0,BUSY
2017-07-26 23:59:37,15aa68bc,30,61,51,OK
2017-07-27 00:00:10,150994b8,66,30,0,BUSY
2017-07-26 23:59:59,15aa6a2c,41,45,14,OK
2017-07-26 23:53:50,14df706d,02,420,419,OK
2017-07-26 23:57:32,14d5b722,22,205,192,OK
2017-07-26 23:58:00,150ff690,35,194,187,OK
2017-07-27 00:00:57,15aa6a2c,44,51,38,OK
2017-07-27 00:01:04,15a4afb7,30,49,48,OK
2017-07-27 00:00:32,15a1c53f,04,85,77,OK
2017-07-27 00:00:46,159fb87c,31,102,94,OK
2017-07-27 00:02:27,15590a6b,02,19,18,OK
2017-07-26 23:59:26,14fa65b7,16,201,178,OK
2017-07-27 00:02:42,15aa6a55,35,52,51,OK
这是我的密码
private List<ColList> colListItems = new List<ColList>();
public List<ColList> readData(string filePath)
{
StreamReader sr = new StreamReader(filePath);
foreach (string readLine in sr.ReadToEnd().Split('\n'))
{
if (readLine != "")
{
string[] line = readLine.Split(',');
colListItems.Add(new ColList()
{
date = line[0],
id = line[1],
bid = int.Parse(line[2]),
time = int.Parse(line[3]),
talk = int.Parse(line[4]),
st = line[5]
});
}
}
return colListItems;
}
public class ColList
{
public string date;
public string id;
public int bid;
public int time;
public int talk;
public string st;
}
会的
2017-07-27 00:00:57 15aa6a2c 0 140 75 OK
并且需要知道(id)重复的次数
对于(id),重复3次
(出价)的最大值和最低值取决于(st==OK)和(st!=OK)的计数
对于这个需求,它就像(30)有4行依赖于st==OK,而(22)有1行
2017-07-27 00:01:04 15a4afb7 30 49 48 OK
2017-07-26 23:59:08 15aa6a2c 30 44 23 OK
2017-07-26 23:56:54 1557ff5c 30 155 155 OK
2017-07-26 23:59:37 15aa68bc 30 61 51 OK
2017-07-26 23:58:00 150ff690 35 194 187 OK
2017-07-27 00:02:42 15aa6a55 35 52 51 OK
2017-07-26 23:59:59 15aa6a2c 41 45 14 OK
2017-07-27 00:00:57 15aa6a2c 44 51 38 OK
2017-07-26 23:53:50 14df706d 02 420 419 OK
2017-07-27 00:02:27 15590a6b 02 19 18 OK
2017-07-26 23:55:56 158db0b1 04 221 219 OK
2017-07-27 00:00:32 15a1c53f 04 85 77 OK
2017-07-27 00:00:46 159fb87c 31 102 94 OK
2017-07-26 23:55:59 14df675e 31 189 168 OK
2017-07-26 23:57:32 14d5b722 22 205 192 OK
通常,您希望使用
GroupBy
方法按Id
对项目进行分组,然后找到符合某些条件的项目
下面是一个示例,该示例应满足以下要求,即在获取具有相同Id的所有项目的Talk总和后,查找Talk值最大的Id:
/// <summary>
/// Will return an item with the Id and Time for the items
/// in the list that have the max Sum(Time) for that id
/// </summary>
/// <param name="input">A list of items to search</param>
/// <returns>A new item representing the Id with the Max(Sum(Time))</returns>
public static ColList GetIdWithMaxTime(List<ColList> input)
{
// Argument validation
if (input == null || !input.Any())
{
throw new ArgumentException("The input list must contain at least one item");
}
// Group items by Id, and select new items with the
// 'Time' field set to the sum of those for that Id
var inputGroupedById = input
.GroupBy(item => item.Id)
.Select(i => new ColList
{
Date = i.Max(item => item.Date),
Id = i.Key,
Time = i.Sum(item => item.Time),
Talk = i.Sum(item => item.Talk),
St = i.First().St
})
.ToList();
// Return the first one whose Time equals the Max(Time)
return inputGroupedById.First(i => i.Time == inputGroupedById.Max(g => g.Time));
}
我还使用此方法解析文本文件,它与您的类似,但添加了一些错误处理:
public static List<ColList> GetData(string filePath)
{
var data = new List<ColList>();
if (filePath == null || !File.Exists(filePath)) return data;
var fileLines = File.ReadAllLines(filePath).Where(line => !string.IsNullOrEmpty(line));
foreach (var fileLine in fileLines)
{
var lineParts = fileLine.Split(',');
int tmp;
data.Add(new ColList()
{
Date = lineParts[0],
Id = lineParts.Length > 0 ? lineParts[1] : "",
BId = lineParts.Length > 1 && int.TryParse(lineParts[2], out tmp) ? tmp : 0,
Time = lineParts.Length > 2 && int.TryParse(lineParts[3], out tmp) ? tmp : 0,
Talk = lineParts.Length > 3 && int.TryParse(lineParts[4], out tmp) ? tmp : 0,
Status = lineParts.Length > 4 ? lineParts[5] : ""
});
}
return data;
}
现在,我们可以通过按时间或通话顺序排列分组项目来显示数据:
Console.WriteLine("Here are the items sorted by Time, followed by the times the Id repeated:");
foreach (var item in groupedItems.OrderByDescending(i => i.Time))
{
// Get the count of this id, and if it was repeated more than once color the text green
var countOfThisId = items.Count(i => i.Id == item.Id);
var consoleColor = countOfThisId > 1 ? ConsoleColor.Green : Console.ForegroundColor;
Console.ForegroundColor = consoleColor;
Console.WriteLine($"{item}\tId was repeated {countOfThisId} times.");
Console.ResetColor();
}
Console.WriteLine("\nHere are the items sorted by Talk:");
foreach (var item in groupedItems.OrderByDescending(i => i.Talk))
{
Console.WriteLine(item);
}
这将提供以下输出:
我们不需要分组项目来获得投标结果。相反,我们可以在Status==“OK”
上筛选原始列表,然后在BId
字段中使用OrderByDescending
对结果排序:
// Order our results on 'BId' field
Console.WriteLine("\nHere are the results ordered by BId where status is 'OK':");
int lastBid = 0;
foreach (var item in items.Where(i =>
i.Status.Equals("OK")).OrderByDescending(i => i.BId))
{
// Put a blank line between groups of BIds
if (item.BId != lastBid)
{
Console.WriteLine();
lastBid = item.BId;
}
Console.WriteLine(item);
}
这将产生以下输出:
“所以我想通过时间和获得最大值来获得具有多个时间的id,并获得具有多个重复日志的id以及用于投标的最大和最小日志。”这是不可能理解的。请你重写一下好吗?@Sach我更新了要求。很抱歉,我还是不明白。也许您可以使用自己的数据并显示示例或输出数据。是否要查找“时间”值最大的项,以及列表中所有“时间”属性的总和?另外,如果“id”是唯一的,根据定义,它不能重复。我想求和,并在我进行求和之后找到id唯一的最大时间,在此之前它不是唯一的
public static List<ColList> GetData(string filePath)
{
var data = new List<ColList>();
if (filePath == null || !File.Exists(filePath)) return data;
var fileLines = File.ReadAllLines(filePath).Where(line => !string.IsNullOrEmpty(line));
foreach (var fileLine in fileLines)
{
var lineParts = fileLine.Split(',');
int tmp;
data.Add(new ColList()
{
Date = lineParts[0],
Id = lineParts.Length > 0 ? lineParts[1] : "",
BId = lineParts.Length > 1 && int.TryParse(lineParts[2], out tmp) ? tmp : 0,
Time = lineParts.Length > 2 && int.TryParse(lineParts[3], out tmp) ? tmp : 0,
Talk = lineParts.Length > 3 && int.TryParse(lineParts[4], out tmp) ? tmp : 0,
Status = lineParts.Length > 4 ? lineParts[5] : ""
});
}
return data;
}
// Populate our colListItems list and get grouped items
var filePath = @"f:\public\temp\temp.txt";
var items = GetData(filePath);
var groupedItems = GroupItemsOnId(items);
Console.WriteLine("Here are the items sorted by Time, followed by the times the Id repeated:");
foreach (var item in groupedItems.OrderByDescending(i => i.Time))
{
// Get the count of this id, and if it was repeated more than once color the text green
var countOfThisId = items.Count(i => i.Id == item.Id);
var consoleColor = countOfThisId > 1 ? ConsoleColor.Green : Console.ForegroundColor;
Console.ForegroundColor = consoleColor;
Console.WriteLine($"{item}\tId was repeated {countOfThisId} times.");
Console.ResetColor();
}
Console.WriteLine("\nHere are the items sorted by Talk:");
foreach (var item in groupedItems.OrderByDescending(i => i.Talk))
{
Console.WriteLine(item);
}
// Order our results on 'BId' field
Console.WriteLine("\nHere are the results ordered by BId where status is 'OK':");
int lastBid = 0;
foreach (var item in items.Where(i =>
i.Status.Equals("OK")).OrderByDescending(i => i.BId))
{
// Put a blank line between groups of BIds
if (item.BId != lastBid)
{
Console.WriteLine();
lastBid = item.BId;
}
Console.WriteLine(item);
}