问题
I have a database containing a table with an "Image" colum:
This column actually contains a long string encoded as HEX byte values.
I need to select all records where the string encoded by this column contains a certain substring. Pseudocode would be:
Select *
From SomeTable
Where dataColumnofTypeImage.ToString().Contains("somesubstring")
I tried to do this in Linq (LinqPad) using:
from z in Zanus
let p = z.Udata.ToArray() // z.Udata will be of type System.Linq.Binary so I want to make a byte array out of it...
where System.Text.ASCIIEncoding.ASCII.GetString(p).Contains("EXED")
select new
{
z.Idnr,
z.Udatum,
z.Uzeit,
z.Unr,
z.Uart,
z.Ubediener,
z.Uzugriff,
z.Ugr,
z.Uflags,
z.Usize,
z.Udata
}
But this does not work at all saying:
NotSupportedException: Method 'Byte[] ToArray()' has no supported translation to SQL.
I just cannot believe it would not be possible to check a binary datatype in a Where clause just as I might check some other datatype...
Can somebody help me out here?
回答1:
The only way I know of doing this is to use direct sql and the Substring
For example, something like :
string str1 = @"
select
Idnr,
Udatum,
Uzeit,
Unr,
Uart,
Ubediener,
Uzugriff,
Ugr,
Uflags,
Usize,
Udata
from Zanus
where Udata is not null
and SubString(Udata, 1 , 2147483647) like '%EXED%'" ;
var query1 = this.ExecuteQuery<Zanus>(str1);
(from z in query1
select new
{
z.Idnr,
z.Udatum,
z.Uzeit,
z.Unr,
z.Uart,
z.Ubediener,
z.Uzugriff,
z.Ugr,
z.Uflags,
z.Usize,
z.Udata
}
Note, this will fail if the search string is in a column, but not in the first 2,147,483,647 bytes (but if these are images, I don't think that should be a concern).
回答2:
where p.Contains(System.Text.ASCIIEncoding.ASCII.GetString("EXED"))
That would presumably be the code for what you're trying to do, whether it would work would depend on your image string
来源:https://stackoverflow.com/questions/6164937/evaluate-image-sql-column-in-a-query