Ok, I know that Select((x, i) => ...) does not have a literal form but I have a fairly complex query which now has a new requirement making half of the projected output fields be dependent on output “row number”. Is there any way, even an ugly one to introduce index and retain one query in literal form?
Also please note that not all source rows participate in result so I cannot just project source into enumerable indexed tuples, indexes have to be applied before final projection and after all the joins and wheres.
Edit:
The original query is large so its pointless to put it here, let ssimplify with pseudo
from a in source
where somecondition(a)
join b in source2 on a.key equals b.key
where someothercondition(a, b)
select new
{
f1 = a.f1,
oldf2 = func(a.field, b.field),
newf2 = func(a.field, b.field, index)
// ... (20 somthing more projected fields)
}
I need index for newf2 and I need it without splitting the query in two queries
If you want this “in one query”, with the majority in “literal form”, you’d have to do something like:
but this is horrible.
I’d prefer to put it all in
.form:The join syntax is more ugly, but at least you’re not mixing up syntaxes.
You might even prefer to do
but that’s not “in one query”…
(This is a lot of LINQ, there may be some syntax errors in there, but you get the general idea.)
BLINDING FLASH OF INSPIRATION
Your mention of
letmagic made me thik of this. It seems to work in a quick example I wrote up.Update: this is rather fragile. For example, if you iterate
querytwice, the index keeps incrementing. (I.e. it doesn’t reset to zero.)