Consider the following linq query:
from r in result
join oUS in onOrderUS on r.ItemGUID equals oUS.ItemGUID into jOnOrderUS
from oUS in jOnOrderUS.DefaultIfEmpty()
let OnOrderUS = oUS != null ? oUS.UnitQty : 0
select (new Func<ItemMinMaxView>(() =>
{
r.OnOrderUS = OnOrderUS;
return r;
})).Invoke()
I want to select r from result, but fill a field with the data from oUS. Is there a better way to do this?
I too would like to see the query split into more logical parts to aid readability. But I would then take advantage of the Reactive Extensions (Rx) to cause the side effect you want (ie the value assignment).
First up I would just create the query like Anthony suggested, but I want to check that your query is correct.
If there are more than one
onOrderUSrecords returned then you’ll only assign the last one found to theItemMinMaxViewrecord.If there will only ever be zero or one record then your query is fine, but it could be simpler.
Either way, try this query instead:
Then I’d get the final results (with the assignment side effect) like this:
The
.Do(...)method is part of Rx and is explicitly there for these kinds of side effects.You do get a load of good extension methods for
IEnumerable<T>in Rx so if you haven’t checked it out you should. It’s even recently been promoted to a supported Microsoft developer tool.