-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: GroupJoin generates LEFT-JOIN and doesn't return all results #6360
Comments
Same root cause as #6318 |
Issue here is: var query = context.Orders
.GroupJoin(context.Customers,
o => o.CustomerID,
c => c.CustomerID,
(o, cs) => new
{
o.OrderID,
Name = cs.Select(c => c.ContactName).FirstOrDefault()
}).ToList(); In above query, customer has one-to-many fk to orders so multiple orders can have same customer. While doing group join with orders being outer, change in outer element (different orders) can have same key which due to our optimization we assume to be same element. Therefore number of groups generated are not same as number of unique outer elements, rather it is equal to number of unique keys. |
Approved for 1.0.1. |
fixed via #6387 |
Steps to reproduce (SQL Server Database)
CREATE TABLE Table_1
(
ID1 int NOT NULL PRIMARY KEY,
ID2 int NULL
);
GO
CREATE TABLE Table_2
(
ID2 int NOT NULL PRIMARY KEY,
MyData int NOT NULL,
MyOtherDate nvarchar(50) NULL
/* I have multi column... */
);
GO
INSERT Table_1 (ID1, ID2) VALUES (11, 21);
INSERT Table_1 (ID1, ID2) VALUES (12, 21);
INSERT Table_1 (ID1, ID2) VALUES (13, 22);
INSERT Table_2 (ID2, MyData, MyOtherDate) VALUES (21, 11111, NULL);
INSERT Table_2 (ID2, MyData, MyOtherDate) VALUES (22, 22222, NULL);
GO
Steps to reproduce (C# Code1)
[Table("Table_1")]
public class Table_1
{
[Key] public int ID1 { get; set; }
public int? ID2 { get; set; }
}
[Table("Table_2")]
public class Table_2
{
[Key] public int ID2 { get; set; }
public int? MyData { get; set; }
public string MyOtherDate { get; set; }
}
//EFCore1-Return only two rows(?!!!)
//SELECT b.[ID2], b.[MyData], b.[MyOtherDate], a.[ID1]
//FROM [Table_1] AS a LEFT JOIN [Table_2] AS b ON a.[ID2] = b.[ID2] ORDER BY a.[ID2]
var x = db.Table_1
.GroupJoin(db.Table_2,
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
MyData = b.Select(c => c.MyData).FirstOrDefault()
})
.ToList();
Steps to reproduce (Alternative Code2)
//EFCore1-ERROR
//System.Data.SqlClient.SqlException
//The column prefix 't20' does not match with a table name or alias name used in the query.
//No column name was specified for column 1 of 't3'.
//Invalid column name 'ID2'.
//Invalid column name 'MyData'.
var y = db.Table_1
.Join(db.Table_2.DefaultIfEmpty(),
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
b.MyData
})
.ToList();
Steps to reproduce (Detect another Error)
//EFCore1-ERROR
//System.InvalidOperationException
//Operation is not valid due to the current state of the object.
var z = db.Table_1
.Join(db.Table_2.Select(r => new { r.ID2, r.MyData }),
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
b.MyData
})
.ToList();
The issue
GroupJoin in Code1 don't return all rows
Row[0]=11,1111
Row[1]=13,2222
Row[2]=12,1111 (don't return)
auto TSQL generate by EFCore1 (return three row):
Join+DefaultIfEmpty in Code2 throw an exception.
Exception type: System.Data.SqlClient.SqlException
Exception message:
The column prefix 't20' does not match with a table name or alias name used in the query.
No column name was specified for column 1 of 't3'.
Invalid column name 'ID2'.
Invalid column name 'MyData'.
Further technical details
"Microsoft.EntityFrameworkCore": "1.0.0"
Sorry for my bad English.
Thanks.
The text was updated successfully, but these errors were encountered: