Given 3 database tables:
A (id, type, code, ...)
B (code, text)
C (id, key, val)
A.code == B.code
A.id == C.id (C.id is not a unique key, multiple entries are possible)
B.text might contain replacement variables, in the form $KEY$
. (So, for example, it might contain: "This is a $SOMEKEY$". C might contain the key and a replacement value:
$SOMEKEY$: Message
$SOMEOTHERKEY$: Text
Currently I perform the replacement in two steps. First, I get all the required data:
var result = from a in context.A where a.type == myType
join b in context.B on a.code == b.code
select new {
ID = a.id,
Code = a.code,
Text = b.Text
};
Next, I get all matching replacement variables. It might be that there is no key available in C.
var data = context.C.Where(c => result.Select(r => r.ID).Contains(c));
Then I loop over the lists and perform the replacement:
foreach (var r in result) {
var local = r;
foreach (var c in data.Where(c => c.id == local.ID)) {
local.Text = local.Text.Replace(c.key, c.val);
}
}
Now, aside from any database compatibility issues and readability aside, would it be possible to perform the search and replace within the first database query?