4

我想知道 listRef 的至少一个元素是否在 listA 中多次出现?其他值可以多次出现。

List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" };
List<string> listRef = new List<string> { "B", "D" };

谢谢,

4

9 回答 9

5

试试这个:

bool hasRef = listref.Any(r => listA.Count(a => a == r) > 1);
于 2013-09-02T09:25:56.380 回答
5

我会先用ToLookup方法生成Lookup<string, string>,然后用它来检查你的情况:

var lookup = listA.ToLookup(x => x);
return listRef.Any(x => lookup.Contains(x) && lookup[x].Count() > 1);

您可以使用GroupByandToDictionary来实现相同的目的:

var groups = listA.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
return listRef.Any(x => groups.ContainsKey(x) && groups[x] > 1);
于 2013-09-02T09:28:34.800 回答
2

像这样的东西

    var query = listRef.Where(x=>
        listA.Where(a => a == x)
             .Skip(1)
             .Any());
于 2013-09-02T09:28:37.487 回答
1
listRef.ForEach(refEl => {

   var count = listA.Count(aEl => aEl == refEl);
   if(count > 1) {

      //Do something

   }

});
于 2013-09-02T09:27:06.930 回答
1

在这种情况下找到性能最佳的选项并不简单,因为这取决于列表中的项目数和预期结果。

这是一种在面对大列表时表现出色的方法:

var appearances = listA.GroupBy(s => s)
                       .Where(g => g.Count() > 1)
                       .ToDictionary(g => g.Key, g => g.Count());
var hasItemAppearingMoreThanOnce = listRef.Any(r => appearances.ContainsKey(r));
于 2013-09-02T09:32:29.543 回答
0

这行得通

    List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" };
    List<string> listRef = new List<string> { "A", "D" };
    foreach (var item in listRef)
    {
        if (listA.Where(x => x.Equals(item)).Count() > 1)
        { 
        //item is present more than once
        }
    }
于 2013-09-02T09:28:47.247 回答
0
var result = listA.GroupBy(x=>x)
                  .Where(g=>g.Count()>1&&listRef.Contains(g.Key))
                  .Select(x=>x.First());
bool a = result.Any();
于 2013-09-02T09:34:12.020 回答
0

这可能是另一种方式

        List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" , "D" };
        List<string> listRef = new List<string> { "B", "D" };

        var duplicates = listA.GroupBy(s => s).SelectMany(grp => grp.Skip(1));
        var newData = duplicates.Select(i => i.ToString()).Intersect(listRef);
于 2013-09-02T09:38:55.800 回答
0

如果第二个列表很大并且可以包含重复项,我将使用 a HashSet<string>andIntersectWith来删除可能的重复项并且strings不在第二个列表中的第一个列表中:

var refSet = new HashSet<string>(listRef);
refSet.IntersectWith(listA);
bool anyMoreThanOne = refSet.Any(rs => listA.ContainsMoreThanOnce(rs, StringComparison.OrdinalIgnoreCase));

这里的扩展不是很优雅但有效:

public static bool ContainsMoreThanOnce(this IEnumerable<string> coll, String value, StringComparison comparer)
{
    if (coll == null) throw new ArgumentNullException("col");
    bool contains = false;
    foreach (string str in coll)
    {
        if (String.Compare(value, str, comparer) == 0)
        {
            if (contains)
                return true;
            else
                contains = true;
        }
    }
    return false;
}

DEMO

但是,如果第二个listRef不大或不包含重复项,则可以使用:

bool anyMoreThanOne = listRef
  .Any(rs => listA.ContainsMoreThanOnce(rs, StringComparison.OrdinalIgnoreCase));
于 2013-09-02T09:41:02.367 回答