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

  • Home
  • SEARCH
  • 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 192267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:24:42+00:00 2026-05-11T16:24:42+00:00

My following C# code is obviously a hack so how do I capture the

  • 0

My following C# code is obviously a hack so how do I capture the index value in the Where method?

     string[] IntArray = { "a", "b", "c", "b", "b"};
                    int index=0;
                    var query = IntArray.Where((s,i) => (s=="b")&((index=i)==i));

    //"&" and "==i" only exists to return a bool after the assignment ofindex

    foreach (string s in query)
                {
                    Console.WriteLine("{0} is the original index of {1}", index, s);
                }
//outputs...
//1 is the original index of b
//3 is the original index of b
//4 is the original index of b
  • 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-05-11T16:24:42+00:00Added an answer on May 11, 2026 at 4:24 pm

    The Where method only returns whether or not the item should be included in the result or not. The function can’t provide any more information in a sensible way (it could capture a local variable and do stuff with it, but that would be horrible).

    If you want the index in the final result, you’ll need to create a projection which includes that index. If you want the original index in the final result, you’ll need to put that projection before any Where clauses.

    Here’s an example of that:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Test
    {
        static void Main()
        {
            IEnumerable<char> letters = "aBCdEFghIJklMNopQRsTUvWyXZ";
    
            var query = letters.Select((c, i) => 
                                       new { Char=c, OriginalIndex=i })
                               .Where(x => char.IsLower(x.Char))
                               .Select((x, i) =>
                                       new { x.Char,
                                             x.OriginalIndex,
                                             FinalIndex=i});
    
            foreach (var result in query)
            {
                Console.WriteLine(result);
            }
        }
    }
    

    Results:

    { Char = a, OriginalIndex = 0, FinalIndex = 0 }
    { Char = d, OriginalIndex = 3, FinalIndex = 1 }
    { Char = g, OriginalIndex = 6, FinalIndex = 2 }
    { Char = h, OriginalIndex = 7, FinalIndex = 3 }
    { Char = k, OriginalIndex = 10, FinalIndex = 4 }
    { Char = l, OriginalIndex = 11, FinalIndex = 5 }
    { Char = o, OriginalIndex = 14, FinalIndex = 6 }
    { Char = p, OriginalIndex = 15, FinalIndex = 7 }
    { Char = s, OriginalIndex = 18, FinalIndex = 8 }
    { Char = v, OriginalIndex = 21, FinalIndex = 9 }
    { Char = y, OriginalIndex = 23, FinalIndex = 10 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.