How to check list A contains any value from list B?

后端 未结 8 703
遇见更好的自我
遇见更好的自我 2021-01-30 08:04

List A:

1, 2, 3, 4

List B:

2, 5

How to check if list A contains any value from list B?

e.g. someth

8条回答
  •  长发绾君心
    2021-01-30 08:56

    I've profiled Justins two solutions. a.Any(a => b.Contains(a)) is fastest.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace AnswersOnSO
    {
        public class Class1
        {
            public static void Main(string []args)
            {
    //            How to check if list A contains any value from list B?
    //            e.g. something like A.contains(a=>a.id = B.id)?
                var a = new List {1,2,3,4};
                var b = new List {2,5};
                var times = 10000000;
    
                DateTime dtAny = DateTime.Now;
                for (var i = 0; i < times; i++)
                {
                    var aContainsBElements = a.Any(b.Contains);
                }
                var timeAny = (DateTime.Now - dtAny).TotalSeconds;
    
                DateTime dtIntersect = DateTime.Now;
                for (var i = 0; i < times; i++)
                {
                    var aContainsBElements = a.Intersect(b).Any();
                }
                var timeIntersect = (DateTime.Now - dtIntersect).TotalSeconds;
    
                // timeAny: 1.1470656 secs
                // timeIn.: 3.1431798 secs
            }
        }
    }
    

提交回复
热议问题