sdk/dotnet/src/Microsoft.Datasync.Client/Query/Linq/QueryTranslator.cs
/// <summary>
/// Add a projection to the query
/// </summary>
/// <param name="expression">A Select Method Call expression</param>
Add a projection to the query
Add a projection to the query
internal protected void AddProjection(MethodCallExpression expression)
        {
            if (expression.IsValidLambdaExpression(out LambdaExpression lambda) && lambda!.Parameters.Count == 1)
            {
                QueryDescription.Projections.Add(lambda.Compile());
                if (QueryDescription.ProjectionArgumentType == null)
                {
                    QueryDescription.ProjectionArgumentType = lambda.Parameters[0].Type;
                    foreach (var memberExpression in lambda.Body.GetMemberExpressions())
                    {
                        string memberName = FilterBuildingExpressionVisitor.GetTableMemberName(memberExpression, ContractResolver);
                        if (memberName != null)
                        {
                            QueryDescription.Selection.Add(memberName);
                        }
                    }
                    JsonContract contract = ContractResolver.ResolveContract(QueryDescription.ProjectionArgumentType);
                    if (contract is JsonObjectContract objectContract)
                    {
                        foreach (string propName in objectContract.Properties.Where(p => p.Required == Required.Always || p.Required == Required.AllowNull).Select(p => p.PropertyName))
                        {
                            if (!QueryDescription.Selection.Contains(propName))
                            {
                                QueryDescription.Selection.Add(propName);
                            }
                        }
                    }
                }
                return;
            }
            throw new NotSupportedException("Invalid projection in 'Select' query expression");
        }
internal protected void AddProjection ( MethodCallExpression expression ) { if ( expression . IsValidLambdaExpression ( out LambdaExpression lambda ) && lambda ! . Parameters . Count == 1 ) { QueryDescription . Projections . Add ( lambda . Compile ( ) ) ; if ( QueryDescription . ProjectionArgumentType == null ) { QueryDescription . ProjectionArgumentType = lambda . Parameters [ 0 ] . Type ; foreach ( var memberExpression in lambda . Body . GetMemberExpressions ( ) ) { string memberName = FilterBuildingExpressionVisitor . GetTableMemberName ( memberExpression , ContractResolver ) ; if ( memberName != null ) { QueryDescription . Selection . Add ( memberName ) ; } } JsonContract contract = ContractResolver . ResolveContract ( QueryDescription . ProjectionArgumentType ) ; if ( contract is JsonObjectContract objectContract ) { foreach ( string propName in objectContract . Properties . Where ( p => p . Required == Required . Always || p . Required == Required . AllowNull ) . Select ( p => p . PropertyName ) ) { if ( ! QueryDescription . Selection . Contains ( propName ) ) { QueryDescription . Selection . Add ( propName ) ; } } } } return ; } throw new NotSupportedException ( " " ) ; }
Add a projection to the query
Add a projection to the query
// We only allow projections consisting of Select(x => ...).  Anything else throws a NotSupportedException // Make sure we also include all the members that would be required for deserialization
expression
MethodCallExpression
A Select Method Call expression
A Select Method Call expression