如何比较两个列表并返回值和索引

时间:2020-12-13 21:41:42

How can I compare two string lists and return this same value and index of the first list?

如何比较两个字符串列表并返回第一个列表的相同值和索引?

Example:

例:

a = List<string> {a,b,c};
b = List<string> {d,a,c};

I need return c and index 3 , a and index 1

我需要返回c和索引3,a和索引1

I am only comparing right now:

我现在才比较:

var duplicateProduct =   productIdList.Where(b => allProduct.Any(a => b.Contains(a)));

Thanks for your help.

谢谢你的帮助。

3 个解决方案

#1


0  

 List<string> listA = new List<string>() { "a", "b", "c" };
 List<string> listB = new List<string>() { "d", "a", "c" };

 var result = listA.Select((x, y) => new { element = x, index = y })
    .Where(z => listB.Contains(z.element));

EDIT: if you don't want a 0 based index, you can do this instead:

编辑:如果你不想要一个基于0的索引,你可以这样做:

var result = listA.Select((x, y) => new { element = x, index = y += 1 })
   .Where(z => listB.Contains(z.element));

EDIT2: get duplicates from both lists

EDIT2:从两个列表中获取重复项

var list1_duplicates = listA.Select((x, y) => new { element = x, index = y })
           .Where(z => listB.Contains(z.element));

var list2_duplicates = listB.Select((x, y) => new { element = x, index = y })
           .Where(z => listA.Contains(z.element));

var all_duplicates = list1_duplicates.Concat(list2_duplicates);

EDIT3: yet another update of the code where we retrieve the indexes on both lists for each element, also managing duplicates in the same list

EDIT3:代码的另一个更新,我们在每个元素的两个列表上检索索引,同时管理同一列表中的重复项

List<string> listA = new List<string>() { "a", "b", "c" };
List<string> listB = new List<string>() { "d", "a", "c", "a" };

var result = listA.Select((a) => new
{
    element = a,
    indexA = listA.IndexOf(a),
    indexB = Enumerable.Range(0, listB.Count).Where(b => listB[b].Equals(a)).ToArray()
});

(optional) Add

(可选)添加

    .Where(a => listB.Contains(a.element));

before the ";" to retrieve only elements present in the second list

之前 ”;”仅检索第二个列表中的元素

#2


1  

var duplicateProduct =   productIdList.Where(b => allProduct.Any(a => b.Contains(a)))
                         .Select(x => new { x, index = productIdList.IndexOf(x)});

This will create an IEnumerable of anonymous types of the item and the index of that item in the productIdList list.

这将在productIdList列表中创建一个IEnumerable的匿名类型的项和该项的索引。

#3


0  

Try this, it might help you.

试试这个,它可能对你有帮助。

var al =new  List<string> { "a","b","c"};
var bl = new List<string> { "d","a","c"};

var res = (from a in al
           join b in bl
           on a equals b
           select new
           {
               Matched = a,
               Index = al.IndexOf(a)
           }).ToList();

#1


0  

 List<string> listA = new List<string>() { "a", "b", "c" };
 List<string> listB = new List<string>() { "d", "a", "c" };

 var result = listA.Select((x, y) => new { element = x, index = y })
    .Where(z => listB.Contains(z.element));

EDIT: if you don't want a 0 based index, you can do this instead:

编辑:如果你不想要一个基于0的索引,你可以这样做:

var result = listA.Select((x, y) => new { element = x, index = y += 1 })
   .Where(z => listB.Contains(z.element));

EDIT2: get duplicates from both lists

EDIT2:从两个列表中获取重复项

var list1_duplicates = listA.Select((x, y) => new { element = x, index = y })
           .Where(z => listB.Contains(z.element));

var list2_duplicates = listB.Select((x, y) => new { element = x, index = y })
           .Where(z => listA.Contains(z.element));

var all_duplicates = list1_duplicates.Concat(list2_duplicates);

EDIT3: yet another update of the code where we retrieve the indexes on both lists for each element, also managing duplicates in the same list

EDIT3:代码的另一个更新,我们在每个元素的两个列表上检索索引,同时管理同一列表中的重复项

List<string> listA = new List<string>() { "a", "b", "c" };
List<string> listB = new List<string>() { "d", "a", "c", "a" };

var result = listA.Select((a) => new
{
    element = a,
    indexA = listA.IndexOf(a),
    indexB = Enumerable.Range(0, listB.Count).Where(b => listB[b].Equals(a)).ToArray()
});

(optional) Add

(可选)添加

    .Where(a => listB.Contains(a.element));

before the ";" to retrieve only elements present in the second list

之前 ”;”仅检索第二个列表中的元素

#2


1  

var duplicateProduct =   productIdList.Where(b => allProduct.Any(a => b.Contains(a)))
                         .Select(x => new { x, index = productIdList.IndexOf(x)});

This will create an IEnumerable of anonymous types of the item and the index of that item in the productIdList list.

这将在productIdList列表中创建一个IEnumerable的匿名类型的项和该项的索引。

#3


0  

Try this, it might help you.

试试这个,它可能对你有帮助。

var al =new  List<string> { "a","b","c"};
var bl = new List<string> { "d","a","c"};

var res = (from a in al
           join b in bl
           on a equals b
           select new
           {
               Matched = a,
               Index = al.IndexOf(a)
           }).ToList();