score:7
In Silverlight, the UIElementCollection
implements IEnumerable<T>
, but the UIElementCollection
in WPF implements IEnumerable
, not IEnumerable<T>
. If you want to use LINQ here, you can
use OfType<UIElement>()
as an option:
foreach (var uiElement in Table.Children.OfType<UIElement>()
.Where(x => Grid.GetColumn((Border)x) == Table.ColumnDefinitions.Count() - 1))
{
//do something
}
score:3
Your code doesn't compile because UIElementCollection implements the non-generic collection interfaces, and Linq requires the generic collection interfaces.
You can use the Cast<T>() method to get an instance of IEnumerable<T>
and then use Linq:
var children = Table.Children.Cast<UIElement>();
foreach (var uiElement in children .Where(x => Grid.GetColumn((Border)x) == Table.ColumnDefinitions.Count() - 1))
{
//do something
}
score:5
The problem is that UIElementCollection
does not implement the generic IEnumerable<T>
:
public class UIElementCollection : IList, ICollection, IEnumerable
While the Enumerable.Where
extension method requires the generic version:
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate
)
So, the solution is to cast the elements first:
foreach (var uiElement in Table.Children.OfType<UIElement>().Where(x => Grid.GetColumn((Border)x) == Table.ColumnDefinitions.Count() - 1))
{
}
Source: stackoverflow.com
Related Query
- Using Where expression on UIElementCollection
- .NET Entity Framework - Using .Contains() to find a byte value in a Where expression
- How to reuse a linq expression for 'Where' when using multiple source tables
- Why are parenthesis needed in F# using method chaining query expression where clause?
- How to build dynamic query with Where and OR using Expression
- using where clause inside include generate error "The Include path expression must refer to a navigation property defined on the type"
- Where clause using Expression <Func<>> and <T>
- using Expression Tree with Where Any EntityCore
- How to write the same code using Lambda Expression
- How to filter children object using WHERE statement in lambda expression
- Why is generated query from expression using case instead of where statements?
- LINQ where clause using Generic IQueryable source
- Using LINQ Lambda expression determining value by group by and where condition
- Where clause using Expression tree builder
- Preserve input with LINQ expression using Except instead of Where
- Using Max, Group By, Join and Where in Lambda Expression
- Build Dynamic Expression For Linq Where using PropertyInfo and a value
- C# Dynamic Expression Tree for search list using Where (or Any) and generics
- Convert string[] to int[] in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- How to SELECT WHERE NOT EXIST using LINQ?
- Entity-framework code is slow when using Include() many times
- Can you create a simple 'EqualityComparer<T>' using a lambda expression
- LINQ - Does the Where expression return new instance or reference to object instance
- LINQ: Get all selected values of a CheckBoxList using a Lambda expression
- How to select array index after Where clause using Linq?
- Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible
- Simple Examples of joining 2 and 3 table using lambda expression
- How to retrieve last 5 records using LINQ method or query expression in C#
More Query from same tag
- linq - format string from two fields in select new context
- How to yield multiple objects with respect to a multi-valued column in Dynamic Linq
- How to change method signature/properties to match given execution example?
- Writing 'CONTAINS' query using LINQ
- DropDownList Items - Free hours
- How do I use LINQ to access a specific value from of an array of JObjects that seems to be stored in a string
- LINQ Query - Display customers with their number of orders including the customer with no orders
- Get count of a datatable column contains with specific value
- c# bind 2 lists to repeater
- Linq select into objects containing collection type variables
- Entity framework is generating SQL that returns the entire table
- Best way to query XDocument with LINQ?
- C# Null coalesce with LINQ
- Using LINQ to select the smallest, most common number in an array
- Remove all empty elements from string array
- How to use anonymous type returned from a method
- EF Code first Eager loading and OrderBy problem
- LINQ to XML Newbie: Moving Nodes From One Node To Another
- Xelement adds element value to itself twice
- Reverse Engineering a LINQ Statement
- Filtering data in LINQ
- Using Linq to check if a boolean expression is true
- Custom LINQ provider for Excel spreadsheets?
- IEnumerable<IDataRecord> comparison
- LINQ group by and combine as CSV
- Converting a 2 Argument Expression into a 1 Argument Expression
- Get raw list from OrderedEnumerable
- How to code summary row in ASP.NET Razor table?
- LINQ to DataTable Easy and Fast
- Do conditional Where filters use deferred execution?