Reputation: 1959
In my xamarin.forms app I am using firebase database for storing users information.I am trying to fetch data between certain date range.
My data model
public class Person
{
public string Name { get; set; }
public int PersonId { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string EmailID { get; set; }
public string Password { get; set; }
public bool Status { get; set; }
public bool IsAdmin { get; set; }
public List<UserLocationData> userdata { get; set; }
}
public partial class UserLocationData
{
public string Latitude { get; set; }
public string Longitude { get; set; }
public DateTime DateTime { get; set; }
}
From my I app I will send two dates (Start Date and End date) so that I would like to get the data which have DateTime
parameter inside this range. How to write the query for that?
What I have tried
In my Firebase helper class,
public async Task<List<Person>> GetPersonLocation(int personId,DateTime startDate,DateTime endDate)
{
return (await firebase
.Child("Persons")
.OrderBy("DateTime")
.StartAt(startDate.ToString("yyyy-MM-dd hh:mm:ss tt"))
.EndAt(endDate.ToString("yyyy-MM-dd hh:mm:ss tt"))
.OnceAsync<Person>()).Select(item => new Person
{
Name = item.Object.Name,
PersonId = item.Object.PersonId,
PhoneNumber = item.Object.PhoneNumber,
EmailID = item.Object.EmailID,
Address = item.Object.Address,
Password = item.Object.Password,
IsAdmin = item.Object.IsAdmin,
Status = item.Object.Status,
userdata=item.Object.userdata
}).ToList();
}
I am getting exception like this :
Response: {
"error" : "Index not defined, add \".indexOn\": \"DateTime\", for path \"/Persons\", to the rules"
}
Which is not seems right.Any help is appreciated.
Upvotes: 0
Views: 576
Reputation: 10346
According to your description, you want to use One to Many relation in sqlite, and query data between two data, am I riht?
If yes, I suggest you can modify your data model, because your two model don't have any relations.
public class person
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public virtual IEnumerable<UserLocationData> userdata { get; set; }
}
public class UserLocationData
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Latitude { get; set; }
public DateTime DateTime { get; set; }
[ForeignKey(typeof(person))]
public int personid { get; set; }
}
Then you can query the data between two date like this:
var persons = await sqliteconn.Table<person>().ToListAsync();
var locations = await sqliteconn.Table<UserLocationData>().ToListAsync();
var query = from p in persons
join location in locations on p.Id equals location.personid
into list1
select new person
{
Id = p.Id,
Name = p.Name,
userdata = list1.Where(s => s.DateTime >= Convert.ToDateTime("2019-02-01") && s.DateTime <= Convert.ToDateTime("2019-03-01"))
};
var list = query.ToList();
There are same thread that you can take a look:
Update:
You can try to get all data from Persons Table for firebase, then see if there are some data in userdata, if yes, now you can filter data
Upvotes: 1