1

I have the following data model:

public class JobPost
{
    public in PostId {get;set;}
    public bool IsRemote {get;set}
}

I'm trying to group the posts by a bool property which doesn't seem to work.

 from p in posts
 group p by new
            {
                IsRemote =  p.IsRemote 
            } into grp
 select new PostViewModel
            {
               IsRemote = grp.Key.IsRemote
            };

Could someone please tell me how to solve this?

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
arif
  • 97
  • 1
  • 11
  • 1
    Clarify "does not work". e.g. Are you getting exception? If yes, what? etc. – Ivan Stoev May 02 '20 at 16:12
  • @Ivan Stoev grouping by a bool column – arif May 02 '20 at 16:15
  • the posted code gives you result not grouped? or an exception? which ef core version you use? – Mohammed Sajid May 02 '20 at 16:23
  • @Sajid yes it throws an excpetion, I', using version 2.2.3 – arif May 02 '20 at 16:48
  • And that doesn't group by a bool. It groups by an anonymous type containing a single read-only property of type bool. – David Browne - Microsoft May 02 '20 at 17:09
  • @arif there is a problem with group by for EF core. but note for 2.1 i think, try to execute the query with and without [.AsEnumerable()](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatableextensions.asenumerable?view=netcore-3.1) : ``var result = from job in posts.AsEnumerable() group job by job.IsRemote into groupedJobs select new { groupedJobs.Key };`` and check the result. – Mohammed Sajid May 02 '20 at 17:18
  • @David Brown, thanks for your answer. Which versions does support this? I upgraded to the latest but it complained on my owned types... – arif May 02 '20 at 17:24
  • @Sajid I don't think AsNumerable() will do because it will execute my database query before grouping.. – arif May 02 '20 at 17:31
  • i think group by work just for <= 2.1 version. for >= 2.2 it's not translated to sql ``AsEnumerable`` just for checking if your code works fine. the same problem for 3.0 version here : https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported. try to use ``ToTraceString`` to check the query. – Mohammed Sajid May 02 '20 at 17:41

1 Answers1

0
public class JobPost
{
    public int PostId {get;set;}
    public bool? IsRemote {get;set}
}

I dont know if this qualifies as a solution but changing my property type from bool to a nullable bool helped.

arif
  • 97
  • 1
  • 11