0
CREATE TABLE IF NOT EXISTS users (
    UserId int auto_increment,
    RegisterId int,
    PRIMARY KEY(UserId)
);

CREATE TABLE IF NOT EXISTS products (
    ProductId int auto_increment,
    ProductName varchar(10),
    UserId int,
    PRIMARY KEY(ProductId)
);
ALTER TABLE products 
ADD CONSTRAINT FK_products_users_UserId
FOREIGN KEY (UserId) REFERENCES users(UserId) ON DELETE CASCADE;

INSERT INTO users values( 1, 3);
INSERT INTO products values (1, "Television", 1) ; 
INSERT INTO products values (2, "IPhone", 1) ;
INSERT INTO products values (3, "Television", 1) ;
using (var db = new someEntities())
{
   var dbReturn = db.users.Join(db.products,
                    x => x.UserId,
                    y => y.UserId,
                    (x, y) => new
                    {
                        x.RegisterId,
                        x.UserId,
                        y.ProductId,
                        y.ProductName
                    })
                    .Where(w => w.UserId == 1)
                    .GroupBy(g => g.RegisterId)
                    .Where(w => w.OrderByDescending(o => o.ProductId)
                          .FirstOrDefault()
                          .ProductName == "Television")
                    .Select(t => new
                    {
                        ProductId = t.Select(s => s.ProductId)
                                        .FirstOrDefault(),
                        ProductName = t.Select(s => s.ProductName)
                                        .FirstOrDefault(),
                        Key = t.Key,
                    })
                    .ToList();
   Console.WriteLine(dbReturn.FirstOrDefault().ProductId);
}

Unexpectedly, i get an error saying

MySqlException: Unknown column 'Join1.RegisterId' in 'where clause'".

When i query (generated sql) in mysql than i get same error as expected.

There is no AS JOIN1 in sql query.

So, what is wrong with my linq query?

SELECT 
1 AS `C1`,
CASE WHEN (`Project8`.`C1` IS NOT NULL) THEN (`Project8`.`C2`)  ELSE (0) END AS `C2`, 
`Project8`.`C3`, 
`Project8`.`RegisterId`
 FROM (SELECT 
`Project6`.`RegisterId`, 
`Project6`.`C1`, 
`Project6`.`C2`, 
(SELECT 
`Extent10`.`ProductName`
 FROM `users` AS `Extent9` INNER JOIN `products` AS `Extent10` ON `Extent9`.`UserId` = `Extent10`.`UserId`
 WHERE (1 = `Extent9`.`UserId`) AND ((`Project6`.`RegisterId` = `Extent9`.`RegisterId`) OR ((`Project6`.`RegisterId` IS  NULL) AND (`Extent9`.`RegisterId` IS  NULL))) LIMIT 1) AS `C3`
 FROM (SELECT 
`Project4`.`RegisterId`, 
`Project4`.`C1`, 
(SELECT 
`Extent8`.`ProductId`
 FROM `users` AS `Extent7` INNER JOIN `products` AS `Extent8` ON `Extent7`.`UserId` = `Extent8`.`UserId`
 WHERE (1 = `Extent7`.`UserId`) AND ((`Project4`.`RegisterId` = `Extent7`.`RegisterId`) OR ((`Project4`.`RegisterId` IS  NULL) AND (`Extent7`.`RegisterId` IS  NULL))) LIMIT 1) AS `C2`
 FROM (SELECT 
`Apply1`.`RegisterId`, 
(SELECT 
`Extent6`.`ProductId`
 FROM `users` AS `Extent5` INNER JOIN `products` AS `Extent6` ON `Extent5`.`UserId` = `Extent6`.`UserId`
 WHERE (1 = `Extent5`.`UserId`) AND ((`Apply1`.`RegisterId` = `Extent5`.`RegisterId`) OR ((`Apply1`.`RegisterId` IS  NULL) AND (`Extent5`.`RegisterId` IS  NULL))) LIMIT 1) AS `C1`
 FROM (SELECT 
`Distinct1`.`RegisterId`, 
(SELECT 
`Project2`.`ProductId`
 FROM (SELECT 
`Extent4`.`ProductId`, 
`Extent4`.`ProductName`
 FROM `users` AS `Extent3` INNER JOIN `products` AS `Extent4` ON `Extent3`.`UserId` = `Extent4`.`UserId`
 WHERE (1 = `Extent3`.`UserId`) AND ((`Join1`.`RegisterId` = `Extent3`.`RegisterId`) OR ((`Join1`.`RegisterId` IS  NULL) AND (`Extent3`.`RegisterId` IS  NULL)))) AS `Project2`
 ORDER BY 
`Project2`.`ProductId` DESC LIMIT 1) AS `ProductId`, 
(SELECT 
`Project2`.`ProductName`
 FROM (SELECT 
`Extent4`.`ProductId`, 
`Extent4`.`ProductName`
 FROM `users` AS `Extent3` INNER JOIN `products` AS `Extent4` ON `Extent3`.`UserId` = `Extent4`.`UserId`
 WHERE (1 = `Extent3`.`UserId`) AND ((`Join1`.`RegisterId` = `Extent3`.`RegisterId`) OR ((`Join1`.`RegisterId` IS  NULL) AND (`Extent3`.`RegisterId` IS  NULL)))) AS `Project2`
 ORDER BY 
`Project2`.`ProductId` DESC LIMIT 1) AS `ProductName`
 FROM (SELECT  DISTINCT 
`Extent1`.`RegisterId`
 FROM `users` AS `Extent1` INNER JOIN `products` AS `Extent2` ON `Extent1`.`UserId` = `Extent2`.`UserId`
 WHERE 1 = `Extent1`.`UserId`) AS `Distinct1`) AS `Apply1`
 WHERE @gp1 = `Apply1`.`ProductName`) AS `Project4`) AS `Project6`) AS `Project8`
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
lowdegeneration
  • 359
  • 5
  • 13

0 Answers0