简体   繁体   English

如何从列表中删除多个元素

[英]how to remove multiple elements from list

This is the list...这是清单...

 List<Location> locations = new List<Location>();
 locations.Add(new Location("Narayana International", 28.6075582, 77.04700729999999));
 locations.Add(new Location("Digital zone", 28.8610328484855, 77.0951520020164));
 locations.Add(new Location("Vijay computers", 28.841364952771706, 77.09076910484801));
 locations.Add(new Location("Codac Info", 29.967759964202948, 76.88406425590289));
 locations.Add(new Location("Balaji computers", 30.6920753011206, 76.80162611916126));
 locations.Add(new Location("Aadi online exam solutions", 30.393033405609803, 76.79088732285474));

Calculate distance with current location then find nearest location and print it on console then remove that location,again find nearest location and print on console then remove the location this itration continue till 4 elements are remove..计算与当前位置的距离,然后找到最近的位置并在控制台上打印,然后删除该位置,再次找到最近的位置并在控制台上打印,然后删除该迭代继续直到 4 个元素被删除的位置。

Now, This is my code which i have tried but how to remove multiple elements..现在,这是我尝试过的代码,但是如何删除多个元素..

List<Location> locations = new List<Location>();
   locations.Add(new Location("Narayana International", 28.6075582, 77.04700729999999));
   locations.Add(new Location("Digital zone", 28.8610328484855, 77.0951520020164));
   locations.Add(new Location("Vijay computers", 28.841364952771706, 77.09076910484801));
   locations.Add(new Location("Codac Info", 29.967759964202948, 76.88406425590289));
   locations.Add(new Location("Balaji computers", 30.6920753011206, 76.80162611916126));
   locations.Add(new Location("Aadi online exam solutions", 30.393033405609803, 76.79088732285474));
  
   double searchLat = 19;
   double searchLong = 74;

   Dictionary<double, List<Location>> distances = new Dictionary<double, List<Location>>();

   locations.ForEach(location =>
   {
       double distance = Program.GetDistance(location.Latitude, location.Longitude, searchLat, searchLong);
       if (distances.ContainsKey(distance))
       {
           distances[distance].Add(location);
       }
       else
       {
           distances.Add(distance, new List<Location>() { location });
       }

   });
   Console.WriteLine("Locations Are:" + locations.Count);
   Console.WriteLine(locations);
   Console.WriteLine("This is the nearest location to me:");

 
   var abc = locations.OrderBy(x => x.Place).ToList();
   double closestDistanceFromSearch = distances.Keys.OrderBy(k => k).First();
   Console.WriteLine(distances[distances.Keys.OrderBy(k => k).First()].Select(x => x.Place).ToList());

  

 locations.RemoveAt(0); // remove object at position 1, in this case 
      Console.WriteLine("Elements are remove:" );
     foreach (var a in locations)
          Console.WriteLine(a.ToString());
  Console.WriteLine("Now the count is : "+ locations.Count  );
           Console.ReadLine();

      


public static double GetDistance(double latitude, double longitude, double otherLatitude, double otherLongitude) {
    var d1 = latitude * (Math.PI / 180.0);
    var num1 = longitude * (Math.PI / 180.0);
    var d2 = otherLatitude * (Math.PI / 180.0);
    var num2 = otherLongitude * (Math.PI / 180.0) - num1;
    var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
   // var d3 = Math.Sqrt((Math.Pow(d1  - d2 , 2) + Math.Pow(num1 - num2, 2)));
   Console.WriteLine(d3);

  return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));

   
}

I would assume the intent is to use a simple loop.我假设目的是使用一个简单的循环。 This can be easily done with MinBy if you are using a recent version of .net:如果您使用的是 .net 的最新版本,则可以使用MinBy轻松完成此操作:

for(int i=0; i < 4; i++){
    var closest = locations.MinBy(l => GetDistance(l.Latitude, l.Longitude, searchLat, searchLong);
    Console.WriteLine(closest.Name);
    locations.Remove(closest);
}

A better solution would be to calculate all the distances to all the locations and do a (partial) sort to get the 4 closest items.更好的解决方案是计算到所有位置的所有距离并进行(部分)排序以获得 4 个最近的项目。 Allowing you to get the same result without the need to change the original collection.让您无需更改原始集合即可获得相同的结果。 But that seem to go against the intent of the assignment.但这似乎违背了任务的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM