1

クエリがあります:

    var groups = query
                .Where(x => x.CrossSearchArticle == searchParameters.SearchString)
                .Join(RuntimeValues.Context.SiteBrands,
                      cross => cross.CrossBrandID,
                      brand => brand.SiteBrandsID,
                      (cross, brand) => new { cross, brand })
                .GroupBy(x => x.cross.CrossGroupID)
                .Select(x => new ProductClarify
                        {
                           CrossGroupID = x.Key,
                           CrossGroupName = x.Max(z => z.cross.CrossGroupName),
                           SourceArticle = x.Max(z => z.cross.SourceArticle),
                           SiteBrandName = x.Max(z => z.brand.Name)
                        });

このクエリは、次のように T-SQL に変換されます。

    SELECT 
    1 AS [C1], 
    [GroupBy1].[K1] AS [CrossGroupID], 
    [GroupBy1].[A1] AS [C2], 
    [GroupBy1].[A2] AS [C3], 
    [GroupBy1].[A3] AS [C4]
    FROM ( SELECT 
        [Extent1].[CrossGroupID] AS [K1], 
        MAX([Extent1].[CrossGroupName]) AS [A1], 
        MAX([Extent1].[SourceArticle]) AS [A2], 
        MAX([Extent2].[Name]) AS [A3]
        FROM  [dbo].[ArticlesCrosses] AS [Extent1]
        INNER JOIN [dbo].[SiteBrands] AS [Extent2] 
           ON [Extent1].[CrossBrandID] = [Extent2].[SiteBrandsID]
        WHERE [Extent1].[CrossSearchArticle] = @p__linq__0
        GROUP BY [Extent1].[CrossGroupID]
    )  AS [GroupBy1]

INNER JOIN を LEFT JOIN に置き換える必要があります。それ以上でもそれ以下でもありません。.DefaultIfEmpty() でいくつかのバリアントを試しましたが、思い通りに動作しません。ここに私の研究があります:

    var groups = query
                .Where(x => x.CrossSearchArticle == searchParameters.SearchString)
                .Join(RuntimeValues.Context.SiteBrands.DefaultIfEmpty(),
                       cross => cross.CrossBrandID,
                       brand => brand.SiteBrandsID,
                       (cross, brand) => new { cross, brand })
                .GroupBy(x => x.cross.CrossGroupID)
                .Select(x => new ProductClarify
                    {
                        CrossGroupID = x.Key,
                        CrossGroupName = x.Max(z => z.cross.CrossGroupName),
                        SourceArticle = x.Max(z => z.cross.SourceArticle),
                        SiteBrandName = x.Max(z => z.brand.Name)
                    });

次のように変換します。

        SELECT 
        1 AS [C1], 
        [GroupBy1].[K1] AS [CrossGroupID], 
        [GroupBy1].[A1] AS [C2], 
        [GroupBy1].[A2] AS [C3], 
        [GroupBy1].[A3] AS [C4]
        FROM ( SELECT 
            [Extent1].[CrossGroupID] AS [K1], 
            MAX([Extent1].[CrossGroupName]) AS [A1], 
            MAX([Extent1].[SourceArticle]) AS [A2], 
            MAX([Join1].[Name]) AS [A3]
            FROM  [dbo].[ArticlesCrosses] AS [Extent1]
            INNER JOIN  (SELECT [Extent2].[SiteBrandsID] AS [SiteBrandsID], 
                                [Extent2].[Name] AS [Name]
                FROM   ( SELECT 1 AS X ) AS [SingleRowTable1]
                LEFT OUTER JOIN [dbo].[SiteBrands] AS [Extent2] ON 1 = 1 ) AS [Join1] 
    ON ([Extent1].[CrossBrandID] = [Join1].[SiteBrandsID]) 
        OR (([Extent1].[CrossBrandID] IS NULL) AND ([Join1].[SiteBrandsID] IS NULL))
            WHERE [Extent1].[CrossSearchArticle] = @p__linq__0
            GROUP BY [Extent1].[CrossGroupID]
        )  AS [GroupBy1]

そしてこれ

    var groups = query
                .Where(x => x.CrossSearchArticle == searchParameters.SearchString)
                    .Join(RuntimeValues.Context.SiteBrands,
                          cross => cross.CrossBrandID,
                          brand => brand.SiteBrandsID,
                          (cross, brand) => new { cross, brand })
                    .DefaultIfEmpty()
                    .GroupBy(x => x.cross.CrossGroupID)
            .Select(x => new ProductClarify
                        {
                           CrossGroupID = x.Key,
                           CrossGroupName = x.Max(z => z.cross.CrossGroupName),
                           SourceArticle = x.Max(z => z.cross.SourceArticle),
                           SiteBrandName = x.Max(z => z.brand.Name)
                        });

に変換します

    SELECT 
    1 AS [C1], 
    [GroupBy1].[K1] AS [CrossGroupID], 
    [GroupBy1].[A1] AS [C2], 
    [GroupBy1].[A2] AS [C3], 
    [GroupBy1].[A3] AS [C4]
    FROM ( SELECT 
        [Project1].[CrossGroupID] AS [K1], 
        MAX([Project1].[CrossGroupName]) AS [A1], 
        MAX([Project1].[SourceArticle]) AS [A2], 
        MAX([Project1].[Name]) AS [A3]
        FROM   ( SELECT 1 AS X ) AS [SingleRowTable1]
        LEFT OUTER JOIN  (SELECT 
            [Extent1].[CrossGroupID] AS [CrossGroupID], 
            [Extent1].[CrossGroupName] AS [CrossGroupName], 
            [Extent1].[SourceArticle] AS [SourceArticle], 
            [Extent2].[Name] AS [Name]
            FROM  [dbo].[ArticlesCrosses] AS [Extent1]
            INNER JOIN [dbo].[SiteBrands] AS [Extent2] 
                       ON [Extent1].[CrossBrandID] = [Extent2].[SiteBrandsID]
            WHERE [Extent1].[CrossSearchArticle] = @p__linq__0 ) AS [Project1] 
                  ON 1 = 1
        GROUP BY [Project1].[CrossGroupID]
    )  AS [GroupBy1]

やり方がわかりません。皆さん、助けが必要です!

4

1 に答える 1

0

ここに騙す方法があります。本当に内部結合を左結合に置き換えたい場合。本番環境ではあまりお勧めできません。それは動作しますが=)

DataContext ctx = yourcontext;
var x = from a in atable
    join b in btableon a.key equals b.key
    select a; //returns atype
string sql = ctx.GetCommand(x).CommandText;
var y = ctx.ExecuteQuery(typeof(atype),sql.Replace("INNER JOIN","LEFT JOIN"));
y.Dump();

;-)

于 2013-03-27T19:24:02.860 に答える