我正在尝试实施分配IP系统,其中MVC Web应用程序的管理员可以分配IP地址,这实际上可以识别位置/工作站,如下图所示
现在,我希望这个用户分配的IP与打开MVC应用程序的机器的IP进行比较,以便从该IP地址发送的任何数据都具有所述的位置。 我想也许是一种搜索或迭代并找到匹配的IP地址的方法,我试图在下面实现它,但坚持如何迭代并找到匹配
public static string LocationIPAssign()
{
var model = new Location();
string workstationLocation = ServerIPName();
foreach (Location location in model.Locations) // ICollection<Location>, IList<Location>
{
if (location.AssignedIP == workstationLocation)
{
// do whatever you have to do with your locationToAssign
}
}
return workstationLocation;
}
}
赞赏任何提示或想法
如果您要求任何进一步的代码,我很乐意提供帮助
已更新 位置模型
[Table("Location")]
public partial class Location
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Location()
{
Jobs = new HashSet<Job>();
}
public int LocationID { get; set; }
[Required]
[StringLength(30)]
[Display(Name = "Location Name")]
public string LocationName { get; set; }
[RegularExpression(@"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$", ErrorMessage = "Invalid IP Address")]
public string AssignedIP { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Job> Jobs { get; set; }
public ICollection<Location> Locations { get; set; }
}
答案 0 :(得分:2)
您也可以使用Lambda表达式。
Locations.where( location => location.AssignedIP == workstationLocation );
答案 1 :(得分:1)
只需遍历您的集合/列表并找到workstationLocation:
public static string LocationIPAssign(Location locationToAssign)
{
string workstationLocation = ComputerIPName();
foreach(Location location in Locations) // ICollection<Location>, IList<Location>
{
if(location.AssignedIP == workstationLocation)
{
// do whatever you have to do with your locationToAssign
}
}
return workstationLocation;
}