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 Articles
- 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#
- How to check value of item is not equal to zero before adding in a list?
- Do LINQ queries use the processing power of the machine running the program or the server hosting the database?
- Entity Framework, Linq : concatenating results from child table
- How to query a Sharepoint 2010 List from Winform Application?
- How to compare to datetime values to millisecond accuracy in LINQ query
- IEqualityComparer<T> does not work with List<T>.Distinct() method
- Client side query builder (jquery) to Entity Framework query
- Foreach on collection cast to IEnumerable work slower than without cast?
- Computed Columns in EF using LINQ
- Replace custom ExpressionVisitor with ReplacingExpressionVisitor
- C# linq expression in lambda with contains
- LINQ group data into a list of objects containing a list + selecting specific original data
- How to update certain columns in db with EF
- LINQ to SQL throws SQL Exception using local collection
- Issue with custom EqualityComparer
- Combine two list using Linq and add data as needed from different tables
- How to get distinct items from a LINQ query in C#?
- failing to write LINQ for my requirements
- Linq where IN using method syntax
- Get dates when quantity were out of stock