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等名称相匹配 – 可能不是您所期望的.
总结以上是内存溢出为你收集整理的c# – 如何有效地使用大数据集中LINQ并行中的子句或选择全部内容,希望文章能够帮你解决c# – 如何有效地使用大数据集中LINQ并行中的子句或选择所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)