-1

it is possible to find some data by using this condition "that have the first letter A"

here is how i use find() to find id_cart that equal with the input

var store = await _cartCollection.Find(x => x.id_cart == entity.id_cart).ToListAsync();

but i want to change it to be like this, but i dont know how

string key = "ID";
var store = await _cartCollection.Find(x => x.id_cart "that have the first character" key).ToListAsync();

is it possible ? how should i do it ?

2 Answers2

1

You can use .StartsWith(),

string key = "ID";
var store = await _cartCollection.Find(x => x.id_cart.StartsWith(key)).ToListAsync();
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
1

In c# and .Net there is a method called StartsWith() which returns a boolean.

Source docks: https://learn.microsoft.com/it-it/dotnet/api/system.string.startswith?view=net-5.0

var store = await _cartCollection.Find(x => x.id_cart.StartsWith(key)).ToListAsync();

If you want to ignore case sensitive just use StringComparer.InvariantCultureIgnoreCase I usually use the Where() just because it's similar to SQL statement and more readable. Hope it solves.

bera
  • 89
  • 1
  • 3