C#Lambda:使用Event& lt; T& gt;()

问题描述:

我在变量"selectedElementArray"中有一个FrameworkElements的ArrayList

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));

这很好.

但是我需要避免存在于"selectedElementArray"中的FrameworkElement,例如parentElement

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.

请帮助....

Binil

你只需要一个 where 子句.

You just need a where clause.

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

要使用 except ,您需要传递 IEnumerable :

selectedElementArray.Cast<FrameworkElement>()
   .Except(new FrameworkElement[]{ parentElement })
   .ToList()
   .ForEach(fe => Canvas.SetTop(fe, top));