Skip to content Skip to sidebar Skip to footer

Xamarin Forms Sqlite Relation

I am going to create an app in Xamarin forms and starting with SQLite. I need to have unique list items for each Main item in the app. For example, I am having a list with items. W

Solution 1:

How about to use Linq and join the relationships. 1.- First you have to add the namespace:

using System.Linq;

2.- Change the property in the class ProfileItems to be a IEnumerable

[OneToMany(CascadeOperations = CascadeOperation.All)]
publicvirtual IEnumerable<LoggItems> Loggs { get; set; }

3.- This is the method to join the loggs with the profile items.

var profiles = await GetProfileAsync();
var loggs = await GetLoggAsync();
var query = from p in profiles
    join l in loggs on p.Id equals l.ProfileId into list
    selectnew ProfileItems
    {
        Id = p.Id,
        ProfileIcon = p.ProfileIcon,
        ProfileName = p.ProfileName,
        ProfileRace = p.ProfileRace,
        BDay = p.BDay,
        Loggs = list
    };

Solution 2:

I think you must add "virtual" keyword for enabling lazy loading.

[OneToMany(CascadeOperations = CascadeOperation.All)]
publicvirtual List<LoggItems> Loggs { get; set; }

And there's the "[InverseProperty]" to specify their related navigation property.

publicclassLoggItems
{
     *
     [ForeignKey(typeof(ProfileItems))]
     [InverseProperty("Loggs")]
     publicint ProfileId { get; set; }
     *
}

Post a Comment for "Xamarin Forms Sqlite Relation"