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.

Kumar
  • 864
  • 1
  • 22
  • 46

1 Answers1

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);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939