'System.InvalidOperationException: Unable to translate set operation when matching columns on both sides have different store types

I have a DTO class which includes 2 nullable fields- DateTime (StartDate) and int (Tid).

   public class TrackSignsDTO
    {
      public int Tsid { get; set; }  
      public int? Tid { get; set; }  
      public DateTime? StartDate { get; set; }
    }

I am using entity framework (.net 6) to concatenate 2 result sets.

The following query is giving an error:

System.InvalidOperationException: Unable to translate set operation when matching columns on both sides have different store types.

        var tracksigns= await _context.MyTracks.AsNoTracking()         
                                .Where(ts=>ts.email==email)
                                .Select(ts=>new TrackSignsDTO
                                       {
                                        Tsid=ts.Id,
                                        Tid=ts.tid,
                                        StartDate=ts.ParentTrack.StartDate
                                       })           
                              .Concat(_context.OtherTracks.AsNoTracking()                                                    
                                   .Where(ot=>ot.email==email)
                                   .Select(ot=>new TrackSignsDTO
                                       {
                                        Tsid=ot.Id,
                                        Tid=null,
                                        StartDate=null                                                            
                                       })
                                ).ToListAsync();
                                

If I remove the StartDate from the DTO and the query, then it works OK. This proves that concat works without error when one of the INT field is null but fails when one of the DateTime field is null. How to fix this?

I am guessing that the error may be because either:

  1. Having one of the side of the result set's column as null is not supported for DateTime column; or
  2. Because in SQL server, the column StartDate of the ParentTrack table is having the NOT NULL setting, where as the DTO has the nullable field.

The other workaround I can think of is making the queries separately and then doing result = list1.concat(list2) but that will put strain onto the resources of the web server (when it should be the responsibility of the sql server to do the concat (aka union all) operation), so I would like to avoid using that approach if possible.

Is there any other workaround to fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source