SO5601397

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
					
public class Program
{
	public static void Main(string[] args)
	{
		var myClass = new List<TestClass> {
			new TestClass() { MyOtherObject = new OtherClass { Name = "Name" } },
			new TestClass() { MyOtherObject = new OtherClass { Name = "John" } },
			new TestClass() { MyOtherObject = new OtherClass { Name = null } },
			new TestClass() { MyOtherObject = null },
			new TestClass() { MyOtherObject = new OtherClass { Name = "Name" } }
		};
		var items = myClass.Where(x => x.MyOtherObject?.Name == "Name").ToList();
		foreach (var item in items)
		{
			Console.WriteLine(item.MyOtherObject.Name);
		}
	}
    
    public class TestClass
    {
        public OtherClass MyOtherObject {get;set;}
    }
    
    public class OtherClass
    {
        public string Name {get;set;}
    }
}
;