0

Say, that I have the following entities defined:

public class User : IdentityUser
{
    public virtual string Name { get; set; }
    public virtual string Surname { get; set; }

    public virtual List<Entry> Entries { get; set; }
}

public class Entry
{
    [Key]
    public virtual int Id { get; set; }

    public virtual string UserId { get; set; }
    public virtual User User { get; set; }

    public virtual long Timestamp { get; set; }
    public virtual string Comment { get; set; }
}

I'd like to retrieve:

  • A list of all users
  • With a list of entries for each user (with empty list if the user has no entries)
  • With entries' Timestamp being between values X and Y

Obviously I can dump Users and Entries tables and organize data by myself, but that would be very inefficient. Is there a way to generate such a result with Entity Framework query?

1

4 Answers 4

3

You cannot filter an Include expression unfortunately, so you will have to create a projection - if there aren't that many properties, it's not too tedious.

   var users = from user in context.Users
     select new User { 
        Name = user.Name,
        Surname = user.Surname,
        Entries = user.Entries.Where(u => u.Timestamp > X && u.Timestamp < Y).ToList()
     }
Sign up to request clarification or add additional context in comments.

1 Comment

This one seems to suit my needs best. Thanks!
0

Try this, it should work as far as i see in your question:

var users = context.Users
    // exclue this where caluse if you want the user to be loaded anyway
    .Where(c => c.Entries.Any(i => i.Timestamp >= X && i.Timestamp <= Y))
    .Select(c => new
    {
        User = c,
        Entries = c.Entries.Where(i => i.Timestamp >= X && i.Timestamp <= Y))
    })
    .FirstOrDefault();

Comments

-1

A list of all users

_dbContext.Users();

With list of entries for each user (with empty list if user has no entries)

 using System.Data.Entity;

_dbContext.Users().Include(x => x.Entries); 

With entries' Timestamp being between values X and Y

using System.Data.Entity;

_dbContext.Users().Where(x => x.Entries.Any(y => y.Timestamp >= X && i.Timestamp <= Y))
.Select(c => new
{
    User = c,
    Entries = c.Entries.Where(x => x.Timestamp >= X && x.Timestamp <= Y))
}).ToList();

1 Comment

EF doesn't support filtered Includes.
-1

You can achieve this by using your database context. I assuming that your database context is using eager loading so you can do something like this:

_yourDataBaseContext.Set<User>()
.Select(it=> new User
{
     Name = it.Name,
     Surname = it.Surname,
     Entries = it.Entries.ToList().Where(e=> e.Timestamp > x && e.Timestamp <y)
})
.Include("Entries")
.ToList();

1 Comment

That gets Users that have any entry with a timestamp within the range, but includes all their entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.