Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/realm/realm-dotnet-groupedcollection
https://github.com/realm/realm-dotnet-groupedcollection
Last synced: 23 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/realm/realm-dotnet-groupedcollection
- Owner: realm
- License: mit
- Created: 2017-06-29T11:03:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-23T14:32:44.000Z (over 7 years ago)
- Last Synced: 2024-11-08T20:08:36.576Z (about 1 month ago)
- Language: C#
- Size: 10.7 KB
- Stars: 8
- Watchers: 7
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-realm - GroupedCollections - Data bindable grouped collections that can be passed directly to a Xamarin.Forms ListView. (<a name="dotnet"> .Net & Unity / <a name="dotnet-libs"></a>.Net Libraries and Components)
README
# GroupedRealmCollection
A set of extension methods that can project [Realm Xamarin](https://realm.io/docs/xamarin/latest/) collections
to data bindable grouped collections that can be passed directly to a [Xamarin.Forms](https://www.xamarin.com/forms) ListView.## Getting started
Just add the [Realm.GroupedCollection](https://www.nuget.org/packages/Realm.GroupedCollection) package to your project.
## Sample usage
```csharp
// In your ViewModel
Students = realm.All().ToGroupedCollection(school => school.Students);
```This can then be passed to your ListView:
```xml
```
And your models would look something like:
```csharp
class Student : RealmObject
{
public string Name { get; set; }
public School School { get; set; }
}class School : RealmObject
{
public string Name { get; set; }
[Backlink(nameof(Student.School))]
public IQueryable Students { get; }
}
```## Features
Both the returned collection and the inner collections are observable, so any changes will update the UI.
### Queries
```csharp
// Return schools ordered by Name, containing Students ordered by Age
var students = realm.All().OrderBy(school => school.Name)
.ToGroupedCollection(school => school.Students.OrderBy(student => student.Age));
```Supported query operations are limited by [Realm's query support](https://realm.io/docs/xamarin/1.5.0/api/linqsupport.html).
### [To-many relationships](https://realm.io/docs/xamarin/latest/#to-many-relationships)
```csharp
public class Dog : RealmObject
{
public string Name { get; set; }
}public class Person : RealmObject
{
public IList Dogs { get; }
}var dogs = realm.All().ToGroupedCollection(person => person.Dogs);
```Note that queries over `IList` properties are not supported and will result in a runtime exception. This is the
case because those queries are executed by LINQ and not by Realm itself.## Using Backlinks to invert the group direction
Usually, a .NET grouping would go from the specific to the general:
```csharp
IEnumerable students = ...;
var groupedStudents = students.GroupBy(s => s.School);
```Since Realm doesn't yet support grouping, we can achieve the same result by using [backlinks (inverse relationships)](https://realm.io/docs/xamarin/latest/#inverse-relationships).
Then, the grouping goes from the general to the specific:
```csharp
IQueryable schools = realm.All();
var groupedStudents = schools.ToGroupedCollection(s => s.Students);
```## License
This project is released under the [MIT license](https://github.com/realm/realm-dotnet-groupedcollection/blob/master/LICENSE).