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().
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 }.
Given that, how can I actually write a LINQ to SQL query to get me the data I want?
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;
might be what your looking for?
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