step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
668 stars 168 forks source link

How to Query FirebaseRTDB? #301

Closed L10Messi10 closed 1 year ago

L10Messi10 commented 1 year ago

I would like to use the query in firebasedatabase like this in SQL Select * From Users where Firstname like " + entryname.Text +" How can I do this in firebasedatabase.net RealtimeDatabase?

I've attached an image on my RTDB

rtdb

jaybowman commented 1 year ago

I hope this will help. First you need to have an index on the property you want to query.

var query = await _fbClient.Child(_userCollection)
                            .OrderBy("firstName")
                            .StartAt("Jimmy")
                            .OnceAsync<Mynamespace.Firebase.User>();

       var list = query.ToList();
L10Messi10 commented 1 year ago

I hope this will help. First you need to have an index on the property you want to query.

var query = await _fbClient.Child(_userCollection)
                            .OrderBy("firstName")
                            .StartAt("Jimmy")
                            .OnceAsync<Mynamespace.Firebase.User>();

       var list = query.ToList();

Hello, @jaybowman Thank you for the reply, after trying your code, seems like it doesn't do the query itself. It just gets the first data of the tree. In my attached image, Querying the data "Allan" the result just fetches the "sdsd" in the data tree, I also added another data with the FirstName "Allan" on the list to confirm my suspicion and I can confirm that it does only fetch the first data.

L10Messi10 commented 1 year ago

I did a little tweak on your code @jaybowman and got things working. This is the code.

//Getting the list of the users ` public async Task<List> GetAllUsers() {

        return (await client
            .Child("Users")
            .OnceAsync<Users>()).Select(item => new Users
        {
            FirstName = item.Object.FirstName,
            LastName = item.Object.LastName
        }).ToList();
    }`

//Applying query to the users with a specific query. First I called the "GetAllUsers" function to fill the List //and then querying that particular List. Tell me if I'm doing it cleverly or if I'm just being Lazy. 😅😅

public async Task<List<Users>> FindUser(string fname) { var queryUsers = await GetAllUsers(); await client .Child("Users") .OnceAsync<Users>(); return queryUsers.Where(a => string.Equals(a.FirstName, fname, StringComparison.CurrentCultureIgnoreCase)).ToList(); }