我有大约250,000条记录标记为Boss,每个Boss有2到10名职员.我每天都需要了解员工的详细信息.大约有1,000,000名员工.我正在使用
Linq获取每日工作人员的唯一列表.考虑以下C#LINQ和模型
void Main() { List<Boss> BossList = new List<Boss>() { new Boss() { EmpID = 101,Name = "Harry",Department = "Development",Gender = "Male",Employees = new List<Person>() { new Person() {EmpID = 102,Name = "Peter",Gender = "Male"},new Person() {EmpID = 103,Name = "Emma Watson",Gender = "Female"},} },new Boss() { EmpID = 104,Name = "Raj",Employees = new List<Person>() { new Person() {EmpID = 105,Name = "Kaliya",..... ~ 250,000 Records ...... }; List<Person> staffList = BossList .SelectMany(x => new[] { new Person { Name = x.Name,Department = x.Department,Gender = x.Gender,EmpID = x.EmpID } } .Concat(x.Employees)) .GroupBy(x => x.EmpID) //Group by employee ID .Select(g => g.First()) //And select a single instance for each unique employee .ToList(); } public class Person { public int EmpID { get; set; } public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } } public class Boss { public int EmpID { get; set; } public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } public List<Person> Employees { get; set; } }
在上面的LINQ中我得到了不同员工或员工名单,该列表包含超过1,000条记录.从获得的列表中我需要搜索“Raj”
staffList.Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant()));
对于此操作,获得结果需要3到5分钟.
我怎么能让它更有效率.请帮助我……
解决方法
如果你改变Boss继承Person(公共类Boss:Person),你不仅不需要在Person和Boss中复制你的属性,你不必为每个Boss创建所有新的Person实例,因为Boss已经是一个人:
IEnumerable<Person> staff = BossList .Concat(BossList .SelectMany(x => x.Employees) ) .DistinctBy(p => p.EmpId) .ToList()
DistinctByis定义为
public static IEnumerable<TSource> DistinctBy<TSource,TKey> (this IEnumerable<TSource> source,Func<TSource,TKey> keySelector) { var seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } }
此外,在您的比较中,您将每个Name转换为小写并进行比较 – 这是您不需要的大量字符串创建.相反,尝试类似的东西
staffList.Where(m => m.Name.Equals("Raj",StringComparison.InvariantCultureIgnoreCase));
此外,请注意,您对Contains的使用也会与Rajamussen和mirajii等名称相匹配 – 可能不是您所期望的.