1- // Copyright (C) 2013 Xtensive LLC.
2- // All rights reserved .
3- // For conditions of distribution and use, see license .
1+ // Copyright (C) 2013-2020 Xtensive LLC.
2+ // This code is distributed under MIT license terms .
3+ // See the License.txt file in the project root for more information .
44// Created by: Alexey Kulakov
55// Created: 2013.12.30
66
1010using System . Linq . Expressions ;
1111using System . Text ;
1212using Xtensive . Core ;
13+ using Xtensive . Orm . Linq . Expressions ;
1314
1415namespace Xtensive . Orm . Linq
1516{
@@ -18,28 +19,34 @@ internal static class LocalCollectionKeyTypeExtractor
1819 public static Type Extract ( BinaryExpression expression )
1920 {
2021 ArgumentValidator . EnsureArgumentNotNull ( expression , "expression" ) ;
22+ if ( expression . Right . StripMarkers ( ) is KeyExpression key ) {
23+ return key . EntityType . UnderlyingType ;
24+ }
25+
2126 var expr = VisitBinaryExpession ( expression ) ;
22- if ( expr . Type . IsSubclassOf ( typeof ( Entity ) ) )
27+ if ( expr . Type . IsSubclassOf ( typeof ( Entity ) ) ) {
2328 return expr . Type ;
29+ }
30+
2431 throw new NotSupportedException ( string . Format ( Strings . ExCurrentTypeXIsNotSupported , expr . Type ) ) ;
2532 }
2633
2734 private static Expression VisitBinaryExpession ( BinaryExpression binaryExpression )
2835 {
29- var memberExpression = binaryExpression . Right as MemberExpression ;
30- if ( memberExpression == null )
31- throw new InvalidOperationException ( string . Format ( Strings . ExCantConvertXToY , binaryExpression . Type , typeof ( MemberExpression ) ) ) ;
36+ if ( ! ( binaryExpression . Right is MemberExpression memberExpression ) ) {
37+ throw new InvalidOperationException ( string . Format ( Strings . ExCantConvertXToY , binaryExpression . Type , typeof ( MemberExpression ) ) ) ;
38+ }
3239 return VisitMemberExpression ( memberExpression ) ;
3340 }
3441
3542 private static Expression VisitMemberExpression ( MemberExpression memberExpression )
3643 {
37- var parameter = memberExpression . Expression as ParameterExpression ;
38- if ( parameter != null )
44+ if ( memberExpression . Expression is ParameterExpression parameter ) {
3945 return parameter ;
40- var member = memberExpression . Expression as MemberExpression ;
41- if ( member != null )
46+ }
47+ if ( memberExpression . Expression is MemberExpression member ) {
4248 return member ;
49+ }
4350 throw new NotSupportedException ( string . Format ( Strings . ExCurrentTypeOfExpressionXIsNotSupported , memberExpression . Expression . Type ) ) ;
4451 }
4552 }
0 commit comments