我有两个2D数组,我试图比较并删除其中一个重复数据。我知道如何从1D数组中删除重复项,但我从未尝试过使用两个2D数组,我希望有人知道一种方法来完成这项工作。对于1D阵列,我会完成img
,但是2d阵列似乎没有相同的工作方式:(这是我正在使用的数据类型:
字段是两者的文字和日期,我的主要目标是能够从" elMultiArray"与" fMultiArray"匹配的那些
这是我尝试完成的一个例子:
alter FUNCTION [dbo].[CalcWorkDaysAddDays_inline](@StartDate As DateTime,@Days AS INT)
returns table
as return
with cte as
(
select *,
ROW_NUMBER() over(order by Calendar_Date) as rn
from RRCP_Calendar
where Calendar_Date > @StartDate and @Days > 0
and not (DATEPART(WEEKDAY,Calendar_Date) IN (1,7) or Is_Holiday = 1)
union ALL
select *,
ROW_NUMBER() over(order by Calendar_Date desc) as rn
from RRCP_Calendar
where Calendar_Date < @StartDate and @Days < 0
and not (DATEPART(WEEKDAY,Calendar_Date) IN (1,7) or Is_Holiday = 1)
)
select cast(Calendar_Date as date) as dt
from cte
where rn = abs(@Days);
有人会这么善意至少给我这方面的指示吗?在此先感谢所有人。
答案 0 :(得分:1)
通常我们在单个LINQ语句中执行此操作。但是LINQ没有2D数组方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
string[,] array1 = { { "123", "09/17/17" }, { "456", "09/17/17" }, { "789", "09/18/17" } };
string[,] array2 = { { "147", "09/17/17" }, { "789", "09/20/17" }, { "123", "09/19/17" } };
//Output: string[,] array 3 = {456, 09/17/17}
List<string> keys = new List<string>();
Dictionary<string, string> output = new Dictionary<string, string>();
// grab the first-dimension values
for(int index =0; index < array2.GetLength(0);index++) {
keys.Add( array2[index, 0]);
}
// compare they first-dimension values that we extracted to the first-dimension values in the
// source array. If no match is found, then it is not a duplicate entry, so record it in
// the output container.
for (int index = 0; index < array1.GetLength(0); index++) {
if (!keys.Contains(array1[index,0])) {
output.Add(array1[index, 0], array1[index, 1]);
}
}
// transforming the output Dictionary<> into a 2D array is left as an excersize for the reader.
// print output
foreach(var key in output.Keys) {
Console.WriteLine($"{key}: {output[key]}");
}
Console.ReadKey(true);
}
}
}
答案 1 :(得分:-1)
linq不支持2D数组,但您可以编写可迭代的Except2D()
并生成Tuple<string, string>
。
您可以使用HashSet<string>
存储重复的密钥。 Add
函数可用于检查密钥是否已存在。
static void Main(string[] args)
{
string[,] array1 = { { "123", "09/17/17" }, { "456", "09/17/17" }, { "789", "09/18/17" } };
string[,] array2 = { { "147", "09/17/17" }, { "789", "09/20/17" }, { "123", "09/19/17" } };
// .ToArray() not required when only iterating once
// and .Length is not required
var elements = array1.Except2D(array2).ToArray();
// make it a 2D array, if required
string[,] combined = new string[elements.Length, 2];
for (int i = 0; i < elements.Length; i++)
{
combined[i, 0] = elements[i].Item1;
combined[i, 1] = elements[i].Item2;
}
Console.ReadLine();
}
static IEnumerable<Tuple<string, string>> Except2D(this string[,] a, string[,] b)
{
HashSet<string> keys = new HashSet<string>();
for (int index = 0; index < a.GetLength(0); index++)
{
if(keys.Add(a[index, 0]))
{
yield return new Tuple<string, string>(a[index, 0], a[index, 1]);
}
}
}