ip地址进行比较(存储为varbinary) [英] Comparing IPAddress (stored as varbinary)

查看:615
本文介绍了ip地址进行比较(存储为varbinary)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的活动表中的ip地址栏。这被存储作为varbinary(16),使得它可以有效地(moreso不是存储为字符串),并且还支持IPv6。当我保存,我基本上得到(新System.Net.IPAddress(127.0.0.1))的值。GetAddressBytes()。

I have an IPAddress column on my Activity table. This is stored as a varbinary(16) so that it can be efficient (moreso than storing as a string) and also support IPv6. When I store, I basically get the value of (new System.Net.IPAddress("127.0.0.1")).GetAddressBytes().

我想成为什么能够做的就是寻找与特定字节,例如开头的所有IP地址127 *。我可以很容易地获得该字节,所以只是假设我能得到新的byte [] {127}。

What I want to be able to do is search for all IP addresses that begin with certain bytes, e.g. "127.*". I can easily get the bytes for that, so just assume that I am able to get new byte[] { 127 }.

鉴于这种情况,我怎么能真正写LINQ到SQL查询来获取我我想要的?

Given that, how can I actually write a LINQ to SQL query to get me the data I want?

不幸的是,我没有StartsWith数据,虽然我基本上是想Activity.Where相当于(A => a.IPAddress.StartsWith(新的byte [] {} 127))。

Sadly, I don't have StartsWith, though I essentially want the equivalent of Activity.Where(a => a.IPAddress.StartsWith(new byte[] { 127 })).

推荐答案

如果返回的数据将作为字节数组,为什么不引用数组的第一个字节?听起来像;

If the data is returned as a byte array, why not reference the first byte of the array? Sounds like;

Activity.Where(a => a.IpAddress[0] == 127);



可能是什么你想找的?

might be what your looking for?

您可以在IP地址存储为十六进制的字符串,其中127.0.0.1 =7F000001那么,如果你想找到开头192.168一个IP地址。*您可以使用

You could store the IP address as a hex string, where 127.0.0.1 = "7F000001" then if you want to find an IP address starting with 192.168.* you can use

Activity.Where(a => a.IpAddress.StartsWith("C0A8"));

这篇关于ip地址进行比较(存储为varbinary)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆