Question

I have an Entries table for entity Entry which contains:

public virtual Member Member { get; set; }

I have a Member table for entity Members with:

public virtual ICollection<Entry> Entry { get; set; }

Entries contains a FK to Members (MemberId) in order to assign entries to specific member IDs, When an entry is created it is created for a specific MemberID.

CONSTRAINT [FK_Entries_ToTable] FOREIGN KEY ([MemberId]) REFERENCES [dbo].[Members] ([ID])

Members contains no FKs (Should it?).

My goal is to sort a list of members based on the number of entries they have WITHIN the past 30 days. I.E. return the whole list of members and then order them from 0 entries to however many they have that have occurred within the past 30 days.

I believe I have the syntax correct for the query (Thanks @DylanCorriveau):

var timeComparison = DateTime.Today.AddDays(-30).Day;
var query = repository.Members.OrderByDescending(p => p.Entry.Select(e => e.TimeStamp.Day <= timeComparison).FirstOrDefault());

The code is compiling but the orderby doesn't seem to be having an affect so I have a feeling it might be how I have the tables/EF set up.

Is this the correct implementation of a one-to-many relationship in EF/Sql?

Is there a way to debug or set breakpoints so that I might see the actual number of entries being pulled so I can tell whether my EF setup is incorrect or my query is?

In order to query the Entries through the Member table is a FK needed? Or does the EFdbContext make this link?

Any help would be greatly appreciated. Thanks in advance.

Was it helpful?

Solution

If I am interpreting your question correctly, you want to order by the Count of Entry for a given member where the Entry meets your timestamp criteria, like this:

var query = repository.Members
                      .OrderByDescending(m => 
                                         m.Entry
                                          .Where(e =>
                                                 e.TimeStamp.Day <= timeComparison))
                                          .Count());

To debug/verify the counts that EF is returning, you can use a slightly modified version of the same, but select an anonymous type that contains the Member Id and a count of entries that match the criteria:

var results = repository.Members
                        .OrderByDescending(m => 
                                           m.Entry
                                            .Where(e =>
                                                   e.TimeStamp.Day <= timeComparison)
                                            .Count())
                        .Select(m => new {
                            MemberId = m.Id,
                            Count = m.Entry.Where(e => 
                                                  e.TimeStamp.Day <= timeComparison)
                                           .Count()
                        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top