1

これら 2 つのステートメントの sql が異なる理由を理解しようとしています (違いは、select ステートメントに SignedXml を追加するか、case ステートメントを使用するかを決定することにあるようです。しかし、なぜですか?)。

myTable.Where(p => p.OwnerID.Equals(owner) && p.FolderID == folderId)
       .Select(source => new SignedNativeAnalysis
       {
          ID = source.ID,
          Name = source.Name,
          Created = source.Created,
          FolderID = source.FolderID,
          Locked = source.Locked,
          Modified = source.Modified,
          OwnerID = source.OwnerID,
          IsSigned = source.SignedXml != null
       });

次のようなものを生成します。

exec sp_executesql N'SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name], 
[Extent1].[Created] AS [Created], 
[Extent1].[FolderID] AS [FolderID], 
[Extent1].[Locked] AS [Locked], 
[Extent1].[Modified] AS [Modified], 
[Extent1].[OwnerID] AS [OwnerID], 
CASE WHEN ([Extent1].[SignedXml] IS NOT NULL) THEN cast(1 as bit) WHEN ([Extent1].[SignedXml] IS NULL) THEN cast(0 as bit) END AS [C1]
FROM [dbo].[NativeAnalyses] AS [Extent1]
WHERE ([Extent1].[OwnerID] = @p__linq__0) AND ([Extent1].[FolderID] = @p__linq__1)',N'@p__linq__0 uniqueidentifier,@p__linq__1 

対選択に関数を使用している場合:

myTable.Where(p => p.OwnerID.Equals(owner) && p.FolderID == folderId)
       .Select(Select);

そして選択は

private SignedNativeAnalysis Select(NativeAnalysis source)
{
      return new SignedNativeAnalysis
      {
        ID = source.ID,
        Name = source.Name,
        Created = source.Created,
        FolderID = source.FolderID,
        Locked = source.Locked,
        Modified = source.Modified,
        OwnerID = source.OwnerID,
        IsSigned = source.SignedXml != null
      };
}

次のようなものを生成します。

exec sp_executesql N'SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name], 
[Extent1].[ModelXml] AS [ModelXml], 
[Extent1].[Created] AS [Created], 
[Extent1].[Modified] AS [Modified], 
[Extent1].[OwnerID] AS [OwnerID], 
[Extent1].[SignedXml] AS [SignedXml], 
[Extent1].[FolderID] AS [FolderID], 
[Extent1].[Locked] AS [Locked]
FROM [dbo].[NativeAnalyses] AS [Extent1]
WHERE ([Extent1].[OwnerID] = @p__linq__0) AND ([Extent1].[FolderID] = @p__linq__1)',N'@p__linq__0 uniqueidentifier,@p__linq__1 

また、SignedNativeAnalysis は NativeAnalysis を 1 つの追加プロパティと共に継承するだけです。

internal class SignedNativeAnalysis : NativeAnalysis
{
   public bool IsSigned { get; set; }
}

更新: 提案に従って、式を使用して必要な結果を達成しようとしましたが、代わりに例外が発生しました:

Expression<Func<NativeAnalysis, SignedNativeAnalysis>> signed = p => Select(p);
myTable.Where(p => p.OwnerID.Equals(owner) && p.FolderID == folderId)
       .Select(signed)

LINQ to Entities はメソッド 'SignedNativeAnalysis Select(OverseerUI.EntityFramework.NativeAnalysis)' メソッドを認識せず、このメソッドはストア式に変換できません。

4

1 に答える 1