0

I have a user table with following attributes:

  • FirstName
  • LastName
  • City
  • CompanyName

I want to query the table with any combination of these attributes and that too partial .

For example: Input - Firstname- "bh" and lastname - "ta" Output -A Record with FirstName "aBHishek" , lastName"gupTA"

How do I do this query without using scan? Does using scanconditions with contains means the read operation is less costly?

I have tried this in C#:

var conditions = new List<ScanCondition>();

if (!string.IsNullOrEmpty(user.FirstName))
{
    var firstName = new ScanCondition("SearchFirstName", ScanOperator.Contains, user.FirstName.ToLower());
    conditions.Add(firstName);
}
 

if (!string.IsNullOrEmpty(user.LastName))
{
    var lastName = new ScanCondition("SearchLastName", ScanOperator.Contains, user.LastName.ToLower());
     conditions.Add(lastName);
}

var allUsers = await _context.ScanAsync<OpenOrderUser>(conditions).GetRemainingAsync();
return allUsers;

it does the trick but I want to move away from Scan and use Query, any suggestions? is this approach optimal?

jarmod
  • 71,565
  • 16
  • 115
  • 122

1 Answers1

0

Another option you have to retrieve data from an Amazon DynamoDB table when using the AWS SDK for .NET V3 is to use PartiQL. This lets you use SELECT statements to retrieve data from a table in Amazon DynamoDB. Its similiar to SQL.

You can read more here:

PartiQL select statements for DynamoDB

YOu can find full .NET Code example here:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/dynamodb/scenarios/PartiQL_Basics_Scenario/PartiQL_Basics_Scenario/PartiQLMethods.cs

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • Interesting , I am looking into this more and will try it out – Abhishek Gupta May 03 '23 at 18:41
  • Be aware that your PartiQL queries are simply going to be converted into the relevant DynamoDB query/scan calls under the covers. It doesn't release you from the obligation to plan your queries and table schema in advance to ensure efficient and performant queries. – jarmod May 04 '23 at 00:47
  • you are right , its very tricky with PartiQL as well , without a proper where condition, it still treats it as a scan. – Abhishek Gupta Jun 08 '23 at 00:01