I am joining 2 in memory collections
var items =
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.DefaultIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
};
However, ideally if one of the elements can’t be found, I’d like to raise an exception, something akin to
throw new System.Exception(el.Value + " cannot be found in cache!");
I was looking at the System.Interactive which offers a Catch extension method but I am unsure how to reference the current ‘el’ in that context. So for example I was wondering about something like
var items = (
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.DefaultIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
})
.ThrowIfEmpty();
but, istm, that that would entail passing the whole set into the extension method rather than raising the exception when the missing value is encountered.
Alternatively, I could replace the DefaultIfEmpty with a ThrowIfEmpty
var items = (
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.ThrowIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
});
Is there a ‘proper’/better way to do this?
You can use GroupJoin. Something like this should work for you:
The GroupJoin resultSelector accepts two arguments: the left key, and the sequence of matching right keys. You can raise an exception if the sequence is empty; one way to achieve that would be to use the Single operator.