I am new to LINQ query and I am struggling to figure out how to get the LINQ query working that produces the same result as this SQL query below. Note that ProductCode table can have duplicated records that is why I use “Top 1”.
CREATE TABLE Product (
ProductID INT,
Name VARCHAR(100),
);
CREATE TABLE ProductCode (
ProductID INT,
CodeName VARCHAR(100),
);
INSERT INTO Product(ProductID, Name) VALUES (1, 'Part1');
INSERT INTO Product(ProductID, Name) VALUES (2, 'Part2');
INSERT INTO ProductCode(ProductID, CodeName) VALUES (1, 'Code1');
INSERT INTO ProductCode(ProductID, CodeName) VALUES (1, 'Code1');
INSERT INTO ProductCode(ProductID, CodeName) VALUES (2, 'Code2');
INSERT INTO ProductCode(ProductID, CodeName) VALUES (3, 'Code3');
SELECT p.*
, (Select Top 1 CodeName FROM ProductCode Where ProductID = p.ProductID) As CodeName
FROM Product p;
Any help would be appreciated.
1