我有两个ATTRIBUT / OBJECT的列表。第一个IS AN INT-ARRAY用于两个ID。
我想在LIST中添加一个新的OBJECT,只有当值 12 和 21 (仅组合)不存在于Ids-Array中时孔列表。 //如果只找到一个ID就可以了 - 只有两个ID的组合需要跳转到ELSE-Case。 :)
* 12和21将在以后变量,因此没有静态值
我不知道为什么,但这不起作用......
if(Id!= Destination.Id&& !(ThisDistances.Exists(x => x.Ids.Contains(12))&& ThisDistances.Exists(x => x .Ids.Contains(21)))) {...}
public class DistanceData
{
public int[] Ids;
public int Distance;
}
protected void MakeDistances(int Id, double GeoLat, double GeoLon, int Limit = 100)
{
int i = 0;
List<DistanceData> ThisDistances = new List<DistanceData>();
foreach (var Destination in Destinations)
{
if (Id != Destination.Id && !(ThisDistances.Exists(x => x.Ids.Contains(12)) && ThisDistances.Exists(x => x.Ids.Contains(21))))
{
ThisDistances.Add(new DistanceData()
{
Ids = new int[2] { Id, Destination.Id },
Distance = (int)Math.Round(6378388 * Math.Acos(Math.Sin(GeoLat) * Math.Sin(Destination.GeoLat) + Math.Cos(GeoLat) * Math.Cos(Destination.GeoLat) * Math.Cos(Destination.GeoLon - GeoLon)), 0, MidpointRounding.AwayFromZero)
});
if (++i > 10) break;
}
}
ThisDistances = ThisDistances.OrderBy(x => x.Distance).Take(Limit).ToList();
foreach (var ThisDistance in ThisDistances)
{
Response.Write("<i><b>" + ThisDistance.Distance + "</b> (" + ThisDistance.Ids[0] + "," + ThisDistance.Ids[1] + ")</i><br />");
}
}
如何检查两个 ID是否不在ObjectList的ID-Array中?
答案 0 :(得分:0)
试试这个:
foreach (var Destination in Destinations)
{
if (Id != Destination.Id && (ThisDistances.Exists(x => x.Ids.Contains(12) && x.Ids.Contains(21)) == false))
{
ThisDistances.Add(new DistanceData()
{
Ids = new int[2] { Id, Destination.Id },
Distance = (int)Math.Round(6378388 * Math.Acos(Math.Sin(GeoLat) * Math.Sin(Destination.GeoLat) + Math.Cos(GeoLat) * Math.Cos(Destination.GeoLat) * Math.Cos(Destination.GeoLon - GeoLon)), 0, MidpointRounding.AwayFromZero)
});
if (++i > 10) break;
}
}