[Apologies for the long winded explanation]
I have inherited an aold application with a SQL database from an (very!) old product and I need to do some reporting against the DB directly. Not entirely sure WHY but this is the structure I have uncovered:
I have a Groups table, and a Reports table.
Groups has a type column, ‘R’ or ‘G’, an integer numElements and an image column called elements
Type ‘R’ means the Group contains Reports, ‘G’ means it contains more Groups.
type = 'G'
elements = 0x2C002600
numelements = 2
result = take chunks of 4 characters,
reverse the last and first two character pairs to get 002C and 0026
convert hex to int to get 44 and 38
44 and 38 are two Group ID’s – I can verify this from within the application itself.
The same applies for type ‘R’ where the ID’s resulting are Reports
Is there any way to Map this behaviour in EF4 OR T-SQL so that I can start to do some sensible queries?
e.g.
If type = G and numElements = 3
split the hex string into 3 chunks of 4 characters,
reverse character pairs,
convert to int,
map to Groups
If type = R and numElements = 11
split the hex string into 11 chunks of 4 characters,
reverse character pairs,
convert to int,
map to Reports
There are thousands of reports and hundreds of groups, so although within the application I can see the groupings, to map them manually would take forever. There is no tree view, so I have to open a group to see what it contains, then open sub-groups (if present) etc. etc.
I know I can code this manually and go through line-by-line abnd extract, split, reverse, convert…but wondered if there was a way I could get T-SQL or EF to do this for me?
EDIT:
Please note, the numElements is variable, since a Group can contain 1:Many Groups OR 1:Many Reports (but not a mixture of them). Therefore the hex string is always:
“0x” + “4 x numElements” characters long
This means that the “hex to int” function needs to be able to return an array or similar, so that 1 Group can be linked to Many Groups/Reports.
The only thing that will help you with both is to create a scalar function which does that split, reverse and convert.
I would create three functions:
Unfortunately you can’t create a Navigation Property based on this only. However you can create a view:
With LINQ 2 SQL I can create a Navigation Property between the view and another table, as long as I define a PrimaryKey for the view, in the model.
Not sure with EF…EF alsoResponse On Question Edit
Since the convert function depends on numElements, an external input and not like the hex param which is a column in the main table and can be read from there, you have no option in creating a NavigationProperty between the main table and Groups or Reports.
Unfortunately your problems don’t stop here. This being a table function (returns a table with a single column of type int), EF can’t use it as a “composable” function, like in the following example:
This would have to generate a SQL statement with a CROSS APPLY operator, but now it can’t do it (EF 2011 June CTE can, but is CTE).
What is left? I think going back to stored procedure is not such a bad thing. Other options depends on your numElements set. If this set is limited in a reasonable way, you still can create views like: