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 Articles
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- 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#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- thread messaging system database schema design
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Type system oddity: Enumerable.Cast<int>()
- Entity Framework 6 Code First Custom Functions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- InvalidOperationException in release-mode of visual studio since using .Net 4.0
- Is it possible to express this code in LINQ?
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- Linq expression with Join, and conditional OrderBy / OrderByDescending
- Get XML node value using .EndsWith
- Stripping data from a EPPlus output, from a date range
- Get empty slot with lowest index from an array
- linq query for finding total leave group by leave type
- “A cycle was detected in the set of changes” with Linq to SQL
- Forgot Password MVC 4
- Sql Server table not updated with LINQ
- Entity Framework query joins and group by issue
- Unable to show data on data grid view
- DataGrid: Adding DataGridCellInfo-instances to the DataGrid.SelectedCells-collection is very slow
- Why are expression constructors hidden in c#
- How to use .Where with lambda / IQueryable
- Sort object by property in JObject
- Select the most recent date in a LINQ query with an inner join
- Extra Columns While Converting Linq Result into Data Table
- Find all possible sequence list from single list c#
- LINQ: dot notation equivalent for JOIN
- Linq 'where' clause returning CS1929
- LINQ Select on a dynamic type which has a property name that contains period