Skip to content

Left Outer Join return less number of rows than it should using Linq #14275

@mnhafiz23

Description

@mnhafiz23

Hi, I'm trying to fetch data from joining two tables but the number of rows that I'm expecting/actual result are less than it should be (actual result should be 12 but I've got only 5 rows) and there is no error. Here is my models:

MetInfo:

public class MetInfo
    {
        [Key]
        public int StnID { get; set; }
        public int MMSI { get; set; }
        public DateTime RecvTime { get; set; }
        public Int16? DAC { get; set; }
        public Int16? FI { get; set; }
        public double Longitude { get; set; }
        public double Latitude { get; set; }    
    }

ShipList:

 public class ShipList
    {
        [Key]
        public int ShipListID { get; set; }
        public int? MMSI { get; set; }
        public string Name { get; set; }
        public int? IMO { get; set; }
        public string CallSign { get; set; }
        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
        public DateTime RecvTime { get; set; }
    }

Here is my query:

(from met in vtsDbContext.MetInfos
 orderby met.RecvTime descending
 group met by met.MMSI into mg
 join sl in vtsDbContext.ShipLists on mg.FirstOrDefault().MMSI equals sl.MMSI into ps from sl in ps.DefaultIfEmpty()
select new { mg.FirstOrDefault().MMSI, mg.FirstOrDefault().RecvTime, sl.Name }).ToList();

I've tried to compare it with EF6 by creating 2 different Console Apps. One is using EF Core and the other is using EF6. EF6 returns the expected results but EF Core does not.

Here is EF Core:
capture

This is EF6:
capture2

Further technical details
EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SQLServer

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions