Group by some columns depending on values in Entity Framework

c# c#-5.0 entity-framework entity-framework-6

Question

The following straightforward sentence appears in my Entity Framework code:

        query = query
                .Where(c => c.NotificationType == NotificationType.AppMessage)
                .GroupBy(c => c.ConversationId)
                .Select(d => d.OrderByDescending(p => p.DateCreated).FirstOrDefault());

It only identifies the newestNotification based on a group, choose latest, and enter conversationId. Easy.

However, I will only accept this ifc.NotificationType == NotificationType.AppMessage When a column other than AppMessage (c.NotificationType <> NotificationType.AppMessage ), I only want the column. What I really want to say is something wonderful, like this:

         query = query
                .Where(c => (c.NotificationType <> NotificationType.AppMessage) 
                || ((c.NotificationType == NotificationType.AppMessage)
                .GroupBy(c => c.ConversationId)
                .Select(d => d.OrderByDescending(p => p.DateCreated).FirstOrDefault()));

However, because the GroupBy/Select is reliant on the first where expression, this is illogical.

How can I fix this?

1
2
10/13/2017 9:41:54 AM

Accepted Answer

ZZZ_tmp
3
10/13/2017 3:08:18 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow