.
null # 6:
myList.Where(x=> x.Property?.Property2 == 5);
:
myList.Where(x=> x.Property != null && x.Property.Property2 == 5);
, , , . , LINQ :
https://msdn.microsoft.com/en-us/library/bb513730.aspx
- , ,
, foreach, ( ), try-catch:
List<MyType> myLstResult = null;
try
{
myLstResult = myList.Where(x=> x.Property.Property2 == 5).ToList();
}
catch(NullReferenceException nullref)
{
throw new ArgumentNullException("MyType.Property must not be null", nullref);
}
, . LINQ, Linq-To-Sql Linq-To-Entities, .
: , ThrowIfArgumentNull, :
public static IEnumerable<TSource> ThrowIfArgumentNull<TSource, TNullable>(this IEnumerable<TSource> enumeration, Func<TSource, TNullable> mightBeNullSelector, string argumentName)
where TNullable : class
{
foreach (TSource item in enumeration)
{
if (mightBeNullSelector(item) == null)
throw new ArgumentNullException(argumentName);
yield return item;
}
}
:
List<MyType> myLstresult = myList
.ThrowIfArgumentNull(x => x.Property, "MyType.Property")
.Where(x => x.Property.Property2 == 5)
.ToList();