2
說,在數組2的在array1正好兩個項目重複我有一個查找使用LINQ C#
Array1 [1,2,3]
和數組Array2 [3,2,4] Array3 [2,16,5]
我需要返回只包含正好有兩個與列表的元素列表來自Array1的整數。在這種情況下,Array2自整數2和3相交; 感謝
說,在數組2的在array1正好兩個項目重複我有一個查找使用LINQ C#
Array1 [1,2,3]
和數組Array2 [3,2,4] Array3 [2,16,5]
我需要返回只包含正好有兩個與列表的元素列表來自Array1的整數。在這種情況下,Array2自整數2和3相交; 感謝
嘗試Where()
和Count()
結合:
var matches = new int[] { 1, 2, 3 };
var data = new List<int[]>
{
new int[] { 3, 2, 4 },
new int[] { 2, 16, 5 }
};
var result = data.Where(x => x.Count(matches.Contains) == 2);
,因爲它是int[]
你可以直接使用.Intersect()
。例如
from a in arrays where a.Intersect(Array1).Count() == 2 select a
//arrays contains Array2 and Array3
如果該列表不是列表中的數組,而是列表中的每個數組都以字段形式存在的對象列表。 對不起,如果這是愚蠢的,但... –
'from a objectsWithArray where a.ArrayProperty.Intesect(...' –