1

I am Searching for Desired output, Here is the right place where I can get that.

I have two List in c# as ids1list ,ids2list

ids1list:
Title
nen154
source

ids2list:
tittl
Title
nen154
WTOC
source 
style

I am comparing these two as var result = ids1list.Intersect(ids2list).ToList();

In result I am getting Common Values Here I want Index of ids2list Which are Common values in ids1list

Some on Suggested me like this,

        ids1list.Select(s => new 
        {
            Id = s, Index = ids2list.IndexOf(s) 
        });

But It not yet worked for me, Can anyone give me proper solution. Because I strucked here.

3
  • what is the type of list? List<string>? Commented Jul 1, 2013 at 15:33
  • So you want those that have the same index? Commented Jul 1, 2013 at 15:34
  • Yes, It is List<String> Commented Jul 1, 2013 at 15:37

1 Answer 1

6

You have to select the second list if you want it's indices:

IEnumerable<int> indices = ids2list
    .Select((s, i) => new{ Str = s, Index = i })
    .Where(x => ids1list.Contains(x.Str))
    .Select(x => x.Index);

Another approach similar to yours:

indices = ids1list
    .Select(s => new{ Str = s, Index = ids2list.IndexOf(s) })
    .Where(x => x.Index >= 0)
    .Select(x=> x.Index);
Sign up to request clarification or add additional context in comments.

6 Comments

I have a feeling that is not what he's looking for.
@newStackExchangeInstance: " I want Index of ids2list Which are Common values in ids1list"
I don't know how you can understand that :P
@newStackExchangeInstance Speaking "New User" is an acquired skill.
@TimSchmelter It seems Very nice, I want to go through it, & I have a Doudbt on this. What is IEnumerable<int> Can I use it as a list. Why because using theseindex I going to fetch Values from third List,Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.