score:2
Accepted answer
First will throw. Use FirstOrDefault
that will return the default<T>
and also null-conditional operator (?.
):
Label lbl = grid.Children.OfType<Label>().FirstOrDefault(k => k.Name=="label");
string result = lbl?.Name.ToString();
Console.WriteLine(result);
However, Since this is a WPF project, I suggest use a MessageBox
or something similar to show the result, instead of Console.WriteLine
, like this:
Add this to your using
directives first:
using System.Windows;
And then:
MessageBox.Show(result);
Source: stackoverflow.com
Related Query
- why does this linq code get exponentially slower when applying First() to projection?
- Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID
- Why is .Contains slow? Most efficient way to get multiple entities by primary key?
- Why does LINQ query throw an exception when I attempt to get a count of a type
- Why is my code doing lazy loading even after I turned it off at every possible point?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Why does this LINQ-to-SQL query get a NotSupportedException?
- Why is this code with PredicateBuilder not working?
- Why do I get a "key already added" error when using the key from a GroupBy in ToDictionary?
- Linq to SQL: Why decimal field get truncated on Insert?
- LINQ Source Code Available
- Why can't I get LINQ to work in my .cshtml files?
- Why do changes made in foreach to a Linq grouping select get ignored unless I add ToList()?
- Why do these three pieces of LINQ code produce different (or erroneous) results?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Why do I get "Sequence contains no elements"?
- Why does Enumerable.Range get iterated twice?
- Why I see so much code duplication in Linq in Reflector?
- Why not reveal the type and identity of the source to the client?
- How to get SQL query into LINQ form in C# code
- Why am I getting an InvalidOperationException with this Linq to Sql method?
- creating Linq to sqlite dbml from DbLinq source code
- How to get and use source of grouping in LINQ?
- ef code first: get entity table name without dataannotations
- C# LINQ How to get a data source from a db?
- Why do I get the error message : doubled attribute?
- Why the DataContext.Connection.ConnectionString can't get the password
- Why do I get 'cannot convert type 'IEnumerable<myType>' to 'myType' on this LINQ query?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- Why did my code work when I changed it from IEnumerable to List?
More Query from same tag
- What are the best blogs for staying up to date on C#, ASP.NET, LINQ, SQL, C++, Ruby, Java, Python?
- Linq - Casting IQueryable to IList returns null - WHY?
- How to apply a filter in a LINQ to SQL expression only if results exist when the filter is applied?
- Replacing strings in a string array efficiently
- Where Not in List<string>
- Linq - Project List<A> to grouped List<B> with a C object within List<B>
- The order of elements vary in an IQueryable, every time I query with the same condition
- Linq / Entity Framework not returning associated entities
- How to use an aggregate function in linq
- linq statuses must return distinct
- linq how to build a where predicate
- LINQ SelectMany and Where extension method ignoring nulls
- RavenDB Map Reduce Transform fold a distinct list of strings
- IQueryable Intersect is currently not supported
- Why does my string not parse properly in my linq to objects query?
- linq to one to many relationship Entity framework
- Selecting max in linq query or rewriting to method chain syntax
- EF Core Exclude Where Multiple Columns are Null or Empty
- Select Max, but only on numeric values with LINQ to Entities
- Is the order of an except and intersect operation always ignorable?
- How Filter Binding Source Connected To linq Query
- Datatable group by with linq in vb.net
- How to get a single object by LINQ? ERROR: The result of a query cannot be enumerated more than once
- Convert Database Linq Query Results to String List
- Cannot use PadLeft() in lambda expression
- How to retrieve data with LINQ to SQL into a weakly-typed object that can be moved around
- CF Entity Framework searching across compound entities
- LINQ to SQL Every Nth Row From Table
- LINQ Select Question
- The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework