I am having issues using the ProjectionExoression field in the DynamoDb Database. I am calling this unfinished code like the following and still get all properties.
var query = Db.FromScan<Domain.Organization>();
//var projections = PrintPropertyNames<Domain.Organization>(",", c => c.Name, c => c.Metadata.LastUpdated, c => c.Metadata.Created);
query.ProjectionExpression = PrintPropertyNames<Domain.Organization>(c => c.Name).Join(", ");
//TODO: Only get the Required Fields
var data = query.Select<Domain.Organization>(x => x.Name).Exec();
if (message.Sort != null)
{
var sort = message.Sort[0];
var sortOrder = sort.Dir.EqualsIgnoreCase("asc") ? "ASC" : "DESC";
if (sort.Field.EqualsIgnoreCase("Name"))
data = sortOrder.EqualsIgnoreCase("ASC")
? data.OrderBy(x => x.Name)
: data.OrderByDescending(x => x.Name);
else if (sort.Field.EqualsIgnoreCase("metadata.lastUpdated"))
{
data = sortOrder.EqualsIgnoreCase("ASC")
? data.OrderBy(x => x.Metadata.LastUpdated)
: data.OrderByDescending(x => x.Metadata.LastUpdated);
}
}
var result = message.ConvertTo<ListOrganizationsCommandResult>();
result.Data = new List<object>(data);
return result;
Please be more specific with the exact Issue and/or Exception thrown (with full StackTrace).
Please also provide a reproducible example which we can run locally to repro the issue.
The issue is that event though I am specifying on the Name in the ProjectionExpression, it is returning all Properties and is not providing any error/exception. The above is the code I am using. But here is the full code file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Http;
using ServiceStack;
using ServiceStack.Aws.DynamoDb;
using SimOpSolutions.Infrastructure;
using SimOpSolutions.Tasks.Command.CommandResult.Organization;
using SimOpSolutions.Tasks.Command.Commands.Organization;
using SimOpSolutions.Tasks.Exceptions;
namespace SimOpSolutions.Tasks.Command.CommandHandlers.Organization
{
public class ListOrganizationsCommandHandler :
IAsyncRequestHandler<ListOrganizationsCommand, ListOrganizationsCommandResult>
{
public ListOrganizationsCommandHandler(IPocoDynamo db, IHttpContextAccessor accessor)
{
Db = db;
User = new TokenUser(accessor.HttpContext.User);
}
public IPocoDynamo Db { get; set; }
public TokenUser User { get; set; }
public static List<string> PrintPropertyNames<T>(params Expression<Func<T, object>>[] properties)
{
var returnValues = new List<string>();
foreach (var p in properties)
{
try
{
var name = string.Empty;
if (p.Body is MemberExpression)
{
name = ((MemberExpression)p.Body).Member.Name;
}
else
{
var op = ((UnaryExpression)p.Body).Operand;
name = ((MemberExpression)op).Member.Name;
}
returnValues.Add(name);
}
catch (Exception ex)
{
}
}
return returnValues;
}
public async Task<ListOrganizationsCommandResult> Handle(ListOrganizationsCommand message)
{
if (User.IsUserInRole(TokenUser.ApplicationRole.GlobalAdministrators))
{
/*message.Take = message.Take == 0 ? 20 : message.Take;
var data = Db.FromScan<Domain.Organization>().Exec(message.Take);
var result = message.ConvertTo<ListOrganizationsCommandResult>();
result.Data = new List<object>(data);
return result;*/
try
{
message.Take = message.Take == 0 ? 20 : message.Take;
var query = Db.FromScan<Domain.Organization>();
//var projections = PrintPropertyNames<Domain.Organization>(",", c => c.Name, c => c.Metadata.LastUpdated, c => c.Metadata.Created);
//query.ProjectionExpression = PrintPropertyNames<Domain.Organization>(c => c.Name).Join(", ");
//TODO: Only get the Required Fields
var data = query.Select<Domain.Organization>().Exec();
if (message.Sort != null)
{
var sort = message.Sort[0];
var sortOrder = sort.Dir.EqualsIgnoreCase("asc") ? "ASC" : "DESC";
if (sort.Field.EqualsIgnoreCase("Name"))
data = sortOrder.EqualsIgnoreCase("ASC")
? data.OrderBy(x => x.Name)
: data.OrderByDescending(x => x.Name);
else if (sort.Field.EqualsIgnoreCase("metadata.lastUpdated"))
{
data = sortOrder.EqualsIgnoreCase("ASC")
? data.OrderBy(x => x.Metadata.LastUpdated)
: data.OrderByDescending(x => x.Metadata.LastUpdated);
}
}
var result = message.ConvertTo<ListOrganizationsCommandResult>();
result.Data = new List<object>(data);
return result;
}
catch(Exception ex)
{
return new ListOrganizationsCommandResult();
}
}
throw new NotAuthorizedException();
}
}
}