我试图获取特定IP地址的国家/地区,IP按范围(长整数)存储在数据库中
我有以下SQL查询:
select * from dbo.ip
where IP_From = ( select max(IP_From)
from dbo.ip
where IP_From <= @ipAddress
)
and IP_To = (
select min(IP_To)
from dbo.ip
where IP_To >= @ipAddress
)
数据库中的数据
IP_From IP_To Registry
16777728 16778239 apnic 1302739200 CN CHN China
16778240 16779263 apnic 1302566400 AU AUS Australia
16779264 16781311 apnic 1302566400 CN CHN China
16781312 16785407 apnic 1302566400 JP JPN Japan
16785408 16793599 apnic 1302566400 CN CHN China
16793600 16809983 apnic 1302566400 JP JPN Japan
16809984 16842751 apnic 1302220800 TH THA Thailand
我正在尝试:
//string strHttpClientIP = System.Web.HttpContext.Current.Request.UserHostAddress;
string strHttpClientIP = "58.8.247.169";
var splitIP = strHttpClientIP.Split('.');
var a = splitIP[0];
var b = splitIP[1];
var c = splitIP[2];
Int64 intIPFrom = FunctionsGlobal.IP2Int(Convert.ToString(a + "." + b + "." + c + ".0"));
Int64 intIPTo = FunctionsGlobal.IP2Int(Convert.ToString(a + "." + b + "." + c + ".255"));
var IPList = db.IpToCountries;
var MaxIpFrom = IPList.Max(i => i.IP_From);
var MinIpTo = IPList.Min(i => i.IP_To);
var QueryCurrency = IPList.Where(i=>i.IP_From == MaxIpFrom && i.IP_To == MinIpTo);
我想转换为Linq c#查询,任何线索?
答案 0 :(得分:1)
类似这样的事情
from ip in IPList
let maxIp = IPList.Where(a=>a.IP_From <= ipAddress).Max(a=>a.IP_From)
let minIp = IPList.Where(a=>a.IP_To >= ipAddress).Min(a=>a.IP_To)
where ip.IP_From == maxIp && ip.IP_To == minIp