Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:32:32+00:00 2026-06-05T05:32:32+00:00

Ok, I know that Select((x, i) => …) does not have a literal form

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:32:34+00:00Added an answer on June 5, 2026 at 5:32 am

    If you want this “in one query”, with the majority in “literal form”, you’d have to do something like:

    from t in
        (
            (
                from a in source
                where somecondition(a)
                join b in source2
                on a.key equals b.key
                where someothercondition(a, b)
                select new { a = a, b = b }
            ).Select((x, i) => new { index = i, a = x.a, b = x.b } )
        )
    select new {
        f1 = t.a.f1,
        oldf2 = func(t.a.field, t.b.field),
        newf2 = func(t.a.field, t.b.field, t.index)
        // ... (20 somthing more projected fields) } 
    

    but this is horrible.

    I’d prefer to put it all in . form:

    a
        .Where(a => somecondition(a))
        .Join(b, a => a.key, b => b.key, (a,b) => new { a = a, b = b })
        .Where(pair => somecondition(pair.a, pair.b))
        .Select((t, i) => new
            {
                f1 = t.a.f1,
                oldf2 = func(t.a.field, t.b.field),
                newf2 = func(t.a.field, t.b.field, i)
                // ...
            });
    

    The join syntax is more ugly, but at least you’re not mixing up syntaxes.

    You might even prefer to do

    var pairs = 
        from a in source
        where somecondition(a)
        join b in source2
        on a.key equals b.key
        where someothercondition(a, b)
        select new { a = a, b = b };
    
    var indexedPairs = pairs.Select((x, i) => new { index = i, a = x.a, b = x.b } );
    
    var projectedIndexedPairs =
        from t in indexedPairs
        select new {
            f1 = t.a.f1,
            oldf2 = func(t.a.field, t.b.field),
            newf2 = func(t.a.field, t.b.field, t.index)
            // ... (20 somthing more projected fields)
            };
    

    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 let magic made me thik of this. It seems to work in a quick example I wrote up.

    // Copious commenting to explain what you're doing.
    int x = 0;
    
    // Copious commenting to explain what you're doing.
    var query =
        from a in source
        where somecondition(a)
        join b in source2
        on a.key equals b.key
        where someothercondition(a, b)
        let i = x++
        select new {
            f1 = a.f1,
            oldf2 = func(a.field, b.field),
            newf2 = func(a.field, b.field, i),
            // ... (20 somthing more projected fields)
            };
    

    Update: this is rather fragile. For example, if you iterate query twice, the index keeps incrementing. (I.e. it doesn’t reset to zero.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that != is not equal, but what does it mean when you
I know that SELECT Now() returns the current DateTime, but how can I just
I know that an invalid pointer leads to undefined behaviour but how does free
I know that Apple does not permit developers to read the phone number of
i know that have alot of question like this one, but i dont know
I have read that after select we use column-names but I have found a
I have a form that lets users select checks, and when submitted, creates a
I know that you can do join statements on SELECT, but can you also
hello i have a select query that is integrated in a search function: i
So I know how to select a text with the mouse and show that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.