0

This is the SQL query I am trying to write with LINQ:

SELECT pd.Description, pd.Name, pd.SKU, pt.FileName,
(SELECT ISNULL(M1.parent, '') + '/' + M2.parent + '/' + M2.slug
    FROM seo AS M1
    JOIN seo AS M2
    ON M1.Slug = M2.Parent
    WHERE M2.SeoId = pd.seoId) as Url
FROM SEO seo
INNER JOIN Products p on seo.SeoID = p.SeoID
INNER JOIN RelatedProducts rp on p.ProductID = rp.ProductID
INNER JOIN Products pd on rp.RelatedID = pd.ProductID
INNER JOIN ProductPhotos pp on pd.ProductID = pp.ProductID
INNER JOIN Photos pt on pp.PhotoID = pt.PhotoID
WHERE seo.slug = 'SlugValue'

This is the LINQ I have:

_database.SEODataSource
                .Join(_database.ProductDataSource, seo => seo.SeoID, p => p.SeoID, (seo, p) => new { seo, p })
                .Join(_database.RelatedProductDataSource, @t => @t.p.ProductID, rp => rp.ProductID, (@t, rp) => new { @t, rp })
                .Join(_database.ProductDataSource, @t => @t.rp.RelatedID, pd => pd.ProductID, (@t, pd) => new { @t, pd })
                .Join(_database.ProductPhotoDataSource, @t => @t.pd.ProductID, pp => pp.ProductID, (@t, pp) => new { @t, pp })
                .Join(_database.PhotoDataSource, @t => @t.pp.PhotoID, pds => pds.PhotoID, (@t, pds) => new { @t, pds })
                .Where(@t => @t.@t.@t.@[email protected] == slug)
                .Select(@t => new ProductLinkDTO
                {
                    ProductDesc = @t.@[email protected],
                    ProductName = @t.@[email protected],
                    ProductSku = @t.@[email protected],
                    ProductImageName = @t.pds.FileName,
                    ProductUrl =  (_database.SEODataSource.Join(_database.SEODataSource, seo1 => seo1.SeoID, seo2 => seo2.SeoID, (seo1, seo2) => new {seo1, seo2})
                                  .Where(@t1 => @t1.seo2.SeoID == @t.@t.@t.@[email protected])
                                  .Select(@t1 => @t1.seo1.Parent ?? "" + "/" + @t1.seo2.Parent + "/" + @t1.seo2.Slug)).FirstOrDefault()
                }).ToList());

This is the query it produces:

SELECT [t3].[Name] AS [ProductName], [t3].[Description] AS [ProductDesc], [t3].[SKU] AS [ProductSku], [t5].[FileName] 
AS [ProductImageName], (
    SELECT TOP (1) [t8].[value]
    FROM (
        SELECT COALESCE([t6].[Parent],((@p1 + [t7].[Parent]) + @p2) + [t7].[Slug]) AS [value], [t7].[SeoID]
        FROM [dbo].[SEO] AS [t6]
        INNER JOIN [dbo].[SEO] AS [t7] ON [t6].[SeoID] = [t7].[SeoID]
        ) AS [t8]
    WHERE [t8].[SeoID] = [t0].[SeoID]
    ) AS [ProductUrl]
FROM [dbo].[SEO] AS [t0]
INNER JOIN [dbo].[Products] AS [t1] ON ([t0].[SeoID]) = [t1].[SeoID]
INNER JOIN [dbo].[RelatedProducts] AS [t2] ON [t1].[ProductID] = [t2].[ProductID]
INNER JOIN [dbo].[Products] AS [t3] ON [t2].[RelatedID] = [t3].[ProductID]
INNER JOIN [dbo].[ProductPhotos] AS [t4] ON [t3].[ProductID] = [t4].[ProductID]
INNER JOIN [dbo].[Photos] AS [t5] ON [t4].[PhotoID] = [t5].[PhotoID]
WHERE [t0].[Slug] = @p0',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 
nvarchar(4000)',@p0=N'KBUIT4255E-42in-Masterpiece-Side-By-Side',@p1=N'/',@p2=N'/'

Any idea what I am doing wrong?

1 Answer 1

3

By changing this:

.Select(@t1 => @t1.seo1.Parent ?? "" + "/" + @t1.seo2.Parent + "/" + @t1.seo2.Slug)).FirstOrDefault()

To:

.Select(@t1 =>  String.Concat(@t1.seo1.Parent ?? "", "/", @t1.seo2.Parent, "/", @t1.seo2.Slug))).SingleOrDefault()

The correct SQL was generated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.