Hello Everyone,
I have a list of objects. I want to filter the objects by their type. Can someone guide me how to do it with GetType() command?
Hello Everyone,
I have a list of objects. I want to filter the objects by their type. Can someone guide me how to do it with GetType() command?
Like this?
private void RunScript(List<object> things, ref object A, ref object B)
{
var dic = new Dictionary<Guid, List<int>>();
for (int i = 0; i < things.Count; i++)
{
var id = things[i].GetType().GUID;
if(dic.ContainsKey(id)){
dic[id].Add(i);
}else{
dic.Add(id, new List<int>(){i});
}
}
var res = new DataTree<object>();
var map = new DataTree<int>();
var j = 0;
foreach(var kvp in dic)
{
var path = new GH_Path(this.Iteration, j);
foreach(var i in kvp.Value)
{
res.Add(things[i], path);
}
map.AddRange(kvp.Value, path);
j++;
}
A = res;
B = map;
}
private void RunScript(List<System.Object> data, ref object A, ref object B)
{
var types = new DataTree<string>();
var objects = new DataTree<object>();
var i = 0;
foreach(var grp in data.GroupBy(d => GH_Convert.ToGoo(d).TypeName))
{
var path = new GH_Path(i);
types.Add(grp.Key, path);
foreach(var o in grp)
objects.Add(o, path);
i++;
}
A = types;
B = objects;
}
GroupByGhType.gh (25.8 KB)
Thanks. Can you please explain here to me? >>>>
data.GroupBy(d => GH_Convert.ToGoo(d).TypeName))
GH_Convert.ToGoo: Converts an object into Goo.
GH_GooProxy.TypeName: Returns the type name that the object represents.
Enumerable.GroupBy: Groups the elements of a sequence according to a specified key selector function.