1

I have setup LiteDb with following models.

public partial class User
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("user_roles")]
    public List<UserRole> UserRoles { get; set; }

    [JsonProperty("created_at")]
    public DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("updated_at")]
    public DateTimeOffset UpdatedAt { get; set; }

}

public class UserRole
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

The following statement, using repository pattern, and using Linq to filter records, works as expected. I get the correct number of records from database.

_repository
    .Query<Models.Offline.User>()
    .ToList()
    .Where(x => x.UserRoles.Any(r => r.Name.Equals("customer")))
    .ToList();

I am trying to query the same records using repository pattern as follows.

_repository
    .Query<Models.Offline.User>()
    .Where(u => u.UserRoles.Any(role => role.Name.Equals("customer")))
    .ToList();`

No records are returned using statement shown above.

What is wrong with my above statement?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
gv1507
  • 47
  • 4
  • 10
  • What is your question? Did you expect any records to be returned? what records? how did any records you expect to find get into your repository? Please edit your answer to provide more detail. – Dan Davies Brackett May 18 '18 at 16:33
  • @DanDaviesBrackett I have updated the question. Thanks for guidance. – gv1507 May 19 '18 at 00:55

1 Answers1

4

If you want access subclass in array, you can use index property:

.Where(x => x.UserRoles[0].Name == "customer")

Or you can use index:

_respository.EnsureIndex("User", "idx_customer", "$.UserRoles[*].Name")

And than search using

_repository.Find<Models.Offline.User>(Query.EQ("idx_customer", name))

mbdavid
  • 1,076
  • 7
  • 8