【发布时间】:2014-09-11 10:48:56
【问题描述】:
要求:我有两个字符串数组。 empDetails 数组包含四个字段,假设字段一是 ID,其他字段是详细信息。 empToRemove 数组包含要删除的员工 ID。创建不包含 empToRomove 数组中存在的 ID 的数组字符串。请注意,我必须使用这段代码,它在 empDetails 中包含超过 100000 个数据,在 empToRemove 中包含超过 20000 个数据。 任何建议都非常合适。
string[] empDetails = { "1,abc,2,11k", "2,de,3,11k", "3,abc,2,18k", "4,abdc,2,12k" };
string[] empToRemove = { "1","3" };
我的解决方案
class Program
{
static void Main(string[] args)
{
string[] empDetails = { "1,abc,2,11k", "2,de,3,11k", "3,abc,2,18k", "4,abdc,2,12k" };
string[] empToRemove = { "1","3" };
//Add emp details in list of employee
List<emp> e = new List<emp>();
foreach (var item in empDetails)
{
Dictionary<int, string> tempEmployee = new Dictionary<int, string>();
int i = 1;
foreach (string details in item.Split(','))
{
tempEmployee.Add(i, details);
i++;
}
e.Add(new emp { ID = int.Parse(tempEmployee[1]), Details1 = tempEmployee[2], Details2 = tempEmployee[3], Details3 = tempEmployee[4] });
}
foreach (string item in empToRemove)
{
emp employeeToRemove = e.Where(x => x.ID == int.Parse(item)).Single();
e.Remove(employeeToRemove);
}
foreach (var item in e)
{
Console.WriteLine(item.ID + item.Details1 + item.Details2 + item.Details3);
}
Console.ReadLine();
}
}
class emp
{
public int ID { get; set; }
public string Details1 { get; set; }
public string Details2 { get; set; }
public string Details3 { get; set; }
}
谢谢
【问题讨论】:
-
如果可行,您需要在code review 上发布。
-
每个人都听说过数据库吗?它们旨在做到这一点。
-
或者这个
empDetails = empDetails.ToList().Select(val => val.Split(new char[] { ',' })).ToList().Where(val => !empToRemove.Contains(val[0])).ToList().Select(val => string.Join(",", val)).ToArray();