1

I have an ArrayList of FrameworkElements in variable "selectedElementArray"

and the below code is used to align controls to top

    double top = 100;
    selectedElementArray.Cast<FrameworkElement>()
        .ToList()
        .ForEach(fe => Canvas.SetTop(fe, top));

this is working fine.

but i need to avoid a FrameworkElement, say parentElement, which exists in "selectedElementArray"

selectedElementArray.Cast<FrameworkElement>()
       .ToList()
       .Except(parentElement)
       .ToList()
       .ForEach(fe => Canvas.SetTop(fe, top));

i tried using "Except". but throwing some exception.

pls help....

Binil

7
  • what kind of exception? are you sure parentElement in the selectedElement array? Commented Dec 23, 2010 at 6:03
  • What exception? The exception will give you help. The Except method expects an IEnumerable<object>, not a single object - that would give you a compile time error. Commented Dec 23, 2010 at 6:04
  • @ArsenMkrt, yes parentElemtn is int selecteElementArray Commented Dec 23, 2010 at 6:08
  • @Kirk, the Exception is The type arguments for method 'System.Linq.Enumerable.Except<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. Commented Dec 23, 2010 at 6:09
  • 2
    binil: That's a compiler error, not an exception. Exceptions only happen when your code is actually running. Commented Dec 23, 2010 at 6:12

2 Answers 2

3

You just need a where clause.

selectedElementArray.Cast<FrameworkElement>()
   .Where(element => element != parentElement)
   .ToList()
   .ForEach(fe => Canvas.SetTop(fe, top));

To use except, you need to pass an IEnumerable:

selectedElementArray.Cast<FrameworkElement>()
   .Except(new FrameworkElement[]{ parentElement })
   .ToList()
   .ForEach(fe => Canvas.SetTop(fe, top));
Sign up to request clarification or add additional context in comments.

Comments

3

Maybe you want something like this?

selectedElementArray.Cast<FrameworkElement>()
       .Where(fe => fe != parentElement)
       .ToList()
       .ForEach(fe => Canvas.SetTop(fe, top));

Or maybe:

foreach (var fe in selectedElementArray.Cast<FrameworkElement>()
                                       .Where(fe => fe != parentElement))
    Canvas.SetTop(fe, top);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.