I have a array (with type of Object) which I'm extracting from a System.Collections.ArrayList. And I'm now trying to cast this object to a int[] so i can use it to compare it with another int[].
Note: I'm currently using .Net Framework 7.0.0
var query = (from el in l
where el.ID_Seal_From != "" && el.ID_Seal_From != null
select new
{
conn = el.Conn_from,
seal = el.ID_Seal_From
}).ToList();
var query2 = (from el in l
where el.ID_Seal_To != "" && el.ID_Seal_To != null
select new
{
conn = el.Conn_to,
seal = el.ID_Seal_To
}).ToList();
var res = query.Concat(query2).ToList();
ArrayList arrLi = new();
List<int> indexOfEqualElements = new();
for (int i = 0; i < res.Count; i )
{
for (int j = 0; j < res.Count; j )
{
if (res[i].seal.CompareTo(res[j].seal) == 0)
{
indexOfEqualElements.Add(j);
}
}
if (Contains(arrLi, indexOfEqualElements.ToArray()) == -1) //to avoid multiple entries
{
arrLi.Add(indexOfEqualElements.ToArray());
}
indexOfEqualElements.Clear();
}
In the "contains" call I'm trying to compare the Elements. For this case i need to avoid, that two equal arrays get added to the list. Because afterwards i need this distinct dataset to continue
CodePudding user response:
I'm sorry for not trying to understand all the code above, but you simply can cast all the values of an ArrayList to an IEnumerable like this:
var arrayList = new ArrayList(2)
{
1,
2
};
var integerEnumerable = arrayList
.Cast<int>();
Feel free to add an .ToArray(), if you like an int[] instead.
CodePudding user response:
ArrayList
is deprecated. If you wanted a list you should use List<int>
. In this instance, it seems you actually need something like a HashSet<int>
.
But to just get distinct elements you can simply use DistinctBy
.
var query = (
from el in l
where el.ID_Seal_From != "" && el.ID_Seal_From != null
select new
{
conn = el.Conn_from,
seal = el.ID_Seal_From
});
var query2 = (
from el in l
where el.ID_Seal_To != "" && el.ID_Seal_To != null
select new
{
conn = el.Conn_to,
seal = el.ID_Seal_To
});
var res = query.Concat(query2).DistinctBy(el => el.seal);