ip地址进行比较(存储为varbinary)

编程入门 行业动态 更新时间:2024-10-28 16:16:16
本文介绍了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)

本文发布于:2023-11-08 02:47:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地址   ip   varbinary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!