fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class TEST
  6. {
  7. public static void Main()
  8. {
  9. //検索元の配列
  10. List<string> array = new List<string> { "red", "blue", "yellow", "pink", "green", "blue", "green", "blue", "red", "blue", "yellow" };
  11.  
  12. List<string> sub1 = new List<string> { "blue", "yellow" }; //ある
  13. List<string> sub2 = new List<string> { "blue", "green" }; //ある
  14. List<string> sub3 = new List<string> { "blue", "hoge" }; //ない
  15.  
  16. Console.WriteLine(string.Join(",", array.Any(sub1)));
  17. Console.WriteLine(string.Join(",", array.Any(sub2)));
  18. Console.WriteLine(string.Join(",", array.Any(sub3)));
  19. }
  20. }
  21.  
  22. public static class IEnumerableExtensions
  23. {
  24. public static IEnumerable<int> Any<T>(this IEnumerable<T> source, IEnumerable<T> target)
  25. {
  26. return source
  27. .Select((v, i) => new
  28. {
  29. v = source.Skip(i).Take(target.Count()),
  30. i = i
  31. })
  32. .Select(x => (x.v.SequenceEqual(target)) ? x.i : -1)
  33. .Where(x => x >= 0);
  34. }
  35. }
Success #stdin #stdout 0.06s 24144KB
stdin
Standard input is empty
stdout
1,9
5