Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 2.96 KB

README.md

File metadata and controls

93 lines (71 loc) · 2.96 KB

FirestoreLINQ

NuGet Version NuGet Downloads

"Buy Me A Coffee"

Gives LINQ support to Firestore database.

Getting started

Define your C# Poco that matches the firestore document

/*Attribute FirestoreCollection is optional, 
We will automatically pluralize the class name while creating CollectionReference.
Student will be converted to Students when pluralized.*/

[FirestoreData, FirestoreCollection("Students")]
public class Student
{
    [FirestoreProperty]
    public List<string> Skills { get; set; }

    [FirestoreProperty]
    public string FirstName { get; set; }

    [FirestoreProperty]
    public string LastName { get; set; }

    [FirestoreProperty]
    public int Age { get; set; }
    
    [FirestoreProperty]
    public AddressInfo Address { get; set; }
    
    [FirestoreData]
    public class AddressInfo
    {
        [FirestoreProperty]
        public string City { get; set; }

        [FirestoreProperty]
        public string Country { get; set; }
    }
}

Add AsQuerable to Collection Reference

public class QueryTest
{
    FirestoreDb db;
    QueryTest()
    {
        db = new FirestoreDbBuilder
        {
            ProjectId = <<Project ID>>,
            JsonCredentials = "<<Path to JSON>>"
        }.Build();
    }

    public void GetYoungers()
    {
        var youngers = db.AsQuerable<Student>()
                         .Where(s => s.Age >= 15 && s.Age <= 25).ToList();
        
        //Above LINQ Expression will be executed in form of following firestore query
        var youngersTranslated = db.Collection("Students")
                                   .WhereGreaterThanOrEqualTo("Age", 15)
                                   .WhereLessThanOrEqualTo("Age", 25)
                                   .Select(x=> x.ConvertTo<Student>())
                                   .ToList();
        
        
        
        var indians = db.AsQuerable<Student>()
                        .Where(s => s.Address.Country == "India").ToList();
        
        //Above LINQ Expression will be executed in form of following firestore query
        var indiansTranslated = db.Collection("Students")
                                   .WhereEqualTo("Address.Country", "India")
                                   .Select(x=> x.ConvertTo<Student>())
                                   .ToList();
    }
}

Here are supported Query Methods

Any, Avg, Count, First, FirstOrDefault, Last, LastOrDefault, Max, MaxBy, Min, MinBy, OrderBy, OrderByDescending, Select, Single, SingleOrDefault, Skip, Sum, Take, Where

Note: Where predicate with OR clause not yet supported.

More features coming soon.