合并两个整数向量

时间:2016-03-10 14:15:52

标签: c# arrays linq

我有两个整数向量。第一个包含我的所有userIds(假设count = 100),第二个包含包含所做某事的用户。我需要一个大小为100的结果数组,如果该用户在第二个列表中,则该值为true,否则该值为false。

让我画出这个:

userVector  filteredUserVector  theResultArray
1001        1001                true
1002        1002                true
1003        1004                false
1004        1006                true
1005                            false
1006                            true
1007                            false

我正在使用这样的代码执行此操作:

 var theResultArray = (FROM user IN userVector  
                              JOIN login IN filteredUserVector ON user equals login INTO prodGroup
                              FROM item IN prodGroup.DefaultIfEmpty()
                              SELECT item != 0).ToArray();

这很有效。但我的问题是:

第一个向量具有185569整数值,并且我在for循环中执行此操作。在每个循环中,filteredUserVector都会发生变化。我的循环计数是40.000并且在30k秒内它因内存异常而崩溃。

代码整个代码块:

loop starts:
    var filteredUserVector=anotherDataSource.select(some where conditions)
     var theResultArray = (FROM user IN userVector  
                              JOIN login IN filteredUserVector ON user equals login INTO prodGroup
                              FROM item IN prodGroup.DefaultIfEmpty()
                              SELECT item != 0).ToArray();

    ..some code..

    bigStore.Add(theResultArray)
loop ends

我希望我能解释一下我的问题。 如何以有效的方式组合?

1 个答案:

答案 0 :(得分:1)

您可以使用HashSet<int> Linq

int[] userVector = new int[] {
  1001, 1002, 1003, 1004, 1005, 1006, 1007,
};

int[] filteredUserVector = new int[] {
  1001, 1002, 1004, 1006
};

....

HashSet<int> filtered = new HashSet<int>(filteredUserVector);

Boolean[] theResultArray = userVector
  .Select(item => filtered.Contains(item))
  .ToArray();

// Test:
// True, True, False, True, False, True, False
Console.Write(String.Join(", ", theResultArray));