Q:

Enterprise Architect - Fastest way to find all elements of a stereotype

We use a method in C# to determine all elements of a stereotypes but this is very slow:
    public List<EA.Element> findElementsByStereotype(string stereotype){
        List<EA.Element> result = new List<EA.Element>();
        String xmlQueryResult = repository.SQLQuery(
                                        "select obj1.object_id " +
                                        "from t_object obj1 " +
                                        "where obj1.stereotype = '" + stereotype + "';");
        XmlDocument xml = Tools.XMLUtil.convertStringToXMLDocument(xmlQueryResult);
        XmlNodeList xnList = xml.SelectNodes("/EADATA/Dataset_0/Data/Row");
        foreach (XmlNode xn in xnList){
            result.Add(repository.GetElementByID(Convert.ToInt32(xn.InnerText)));
        }
        return result;
    }

Do you know a more performant solution?

A:

If you want to go fast, XML is never your friend. Luckily, the EA API provides a different way of retrieving a set of elements: Repository.GetElementSet(), which returns an EA.Collection of EA.Elements from either a comma-separated list of element IDs, or from an SQL query.
public List<EA.Element> findElementsByStereotype(string stereotype) {
    List<EA.Element> result = new List<EA.Element>();
    foreach (EA.Element element in repository.GetElementSet("select Object_ID " +
             "from t_object where Stereotype='" + stereotype + "'", 2)) {
        result.Add(element);
    }
    return result;
}

Please note that I haven't verified that this runs faster. I'm assuming it would, but it's possible that GetElementSet() does essentially the same XML parsing that you're doing under the hood. But if nothing else, it's shorter.

