Let's Assume I have Multi dimensional Object array that represents simple matrix
Object[,] mybaseobject = new object[,] {
{"Rakesh", "Martin", "21"},
{"Rohith", "M", "22"},
{"Rakesh", "Martin", "21"},
{"varsha", "MJ", "18"},
Even if String object[,] also fits fine for my project
It Object array looks like
Rakesh Martin 21
Rohith M 22
Rakesh Martin 21
Rohith M 22
varsha Mj 18
Rohith M 22
Please find me way to delete duplicate rows and make array to look like this?
Rakesh Murthy 21
Rohith M 22
varsha Mj 18
I am using this approach [one of existing approach in stack over flow forum ][1]
Delete duplicate rows from two dimentsional array
But in abovelink works fine for Multidimensional Integer data I need solution for Multidimensional object array[,]
Please help me out thanks(+1) in advance
As others have pointed out, you may not want to work with object[,]
. Below is an implementation that transforms the object[,]
to List<object[]>
, performs filtering on that, then transforms the List<object[]>
back to a two dimensional array.
void Filter()
{
Object[,] mybaseobject = new object[,]
{
{ "Ranjith", "Murthy" },
{ "Rohith", "M" },
{ "Ranjith","Murthy" },
{ "varsha", "MJ" }
};
var objectList = AsList(mybaseobject);
var distinctLines = new List<object[]>();
foreach (var line in objectList)
{
if (!distinctLines.Any(x => x.SequenceEqual(line)))
{
distinctLines.Add(line);
}
}
var filteredobjects = AsTwoDimentionalArray(distinctLines);
}
private List<object[]> AsList(object[,] input)
{
var lines = new List<object[]>();
for(var i = 0; i < input.GetLength(0);++i)
{
var line = new List<object>( input.GetLength(1));
for (var j = 0; j < input.GetLength(1); ++j)
{
line.Add(input[i, j]);
}
lines.Add(line.ToArray());
}
return lines;
}
private object[,] AsTwoDimentionalArray(List<object[]> input)
{
var result = new object[input.Count(), input[0].Length];
for (var i = 0; i < input.Count; ++i)
{
for(var j = 0; j < input[0].Length; ++j)
{
result[i, j] = input[i][j];
}
}
return result;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments