FilteredElementCollector inside foreach loop

FilteredElementCollector inside foreach loop

jjesusdelpino
Advocate Advocate
1,311 Views
2 Replies
Message 1 of 3

FilteredElementCollector inside foreach loop

jjesusdelpino
Advocate
Advocate

Im stuck with something appearently simple but I dont know how to handle it. What Im trying is, 

having a list of views, obtain the elements in these views, eg:

 

            foreach (View V in Views)
                List<Element> ListElements = new FilteredElementCollector(doc, V.Id);

 

It raise error CS1023, embedded statement cannot be a declaration or labeled statement. 

 

What I understand here is that I cannot declare List or Enum inside a foreach loop right? But in this case, how can I extract this elements?

 

 

I have also tried declaring the List/Enum before...

 

            List<Element> ListElements = new List<Element>();            
            foreach (View V in Views)
                ListElements.Add(FilteredElementCollector(doc, V.Id));

 

but then it raise error CS0118, is type but is used like a variable. Any ideas? Thanks!

0 Likes
Accepted solutions (1)
1,312 Views
2 Replies
Replies (2)
Message 2 of 3

Moustafa_K
Collaborator
Collaborator
Accepted solution

no not exactly you can't.. infact you can declare any type under if, but it must be scoped, because you are declaring under a condition. so the correct way is to add two egyptian brackets

 foreach (View V in Views)
              {
  List<Element> ListElements = new FilteredElementCollector(doc, V.Id);
// rest of your code }
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 3 of 3

jjesusdelpino
Advocate
Advocate

Wow! I feel so stupid right now. As I wasn´t understing the error I didnt notice I was missing the brackets. Thanks for your anwser!

 

code:

            foreach (View V in VistasLeyenda)
            {
                IEnumerable<Element> Elementos = new FilteredElementCollector(doc,V.Id);
            }

0 Likes