如何在ExceuteCommand ExecuteQuery中使用Iqueryable对象

时间:2015-02-07 18:52:06

标签: c# .net sql-server linq linq-to-sql

我有一张包含以下定义的表格。

Create table RecipientInGroups
{
  ID uniqueidentifier,
  GroupID uniqueidentifier,
  RID uniqueidentifier,
  CreateDt datetime
}

我正在尝试运行以下代码:

var strCommandText = dc.GetCommand(finalRecp).CommandText;

strCommandText = String.Format("INSERT INTO RecipientInGroups SELECT '{0}', '{1}', '{2}', '{3}'", Guid.NewGuid(), gGroupId, finalRecp, now);

dc.ExecuteQuery<EmailRecipientInGroup>(strCommandText);

其中finalRecp是基于特定条件创建的可查询对象。

但是我收到了finalRecp的错误:

  

错误2015-02-08 00:04:39,070 - [CopyContcatsToGroup] -   System.Data.SqlClient.SqlException(0x80131904):转换失败   从字符串转换为uniqueidentifier时。 at   System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   布尔值breakConnection,Action 1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)at   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,   SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj,布尔&amp; dataReady)at   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串   methodName,Boolean async,Int32 timeout,Boolean asyncWrite)at   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1   completion,String methodName,Boolean sendToPipe,Int32 timeout,   布尔asyncWrite)at   System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at   System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询,   QueryInfo queryInfo,IObjectReaderFactory factory,Object []   parentArgs,Object [] userArgs,ICompiledSubQuery [] subQueries,Object   lastResult)at   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询,   QueryInfo [] queryInfos,IObjectReaderFactory factory,Object []   userArguments,ICompiledSubQuery [] subQueries)at   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式   查询)在System.Data.Linq.DataContext.ExecuteMethodCall(Object   实例,MethodInfo methodInfo,Object []参数)at   System.Data.Linq.DataContext.ExecuteCommand(String command,Object []   参数)

如何使用finalRecp(IQueryable)对象在ExecuteQuery或ExecuteCommand中插入值?我如何为使用ExecuteQuery或ExecuteCommand插入的每个RID创建新的ID?

假设我的问题让很多人感到困惑,那么生成的最终查询如下:

INSERT INTO RecipientInGroups SELECT '404f8892-8a1a-4d65-abc1-174c41ce89f7', '662f4f05-0d9f-4036-b048-74178413f033', 'SELECT DISTINCT [t0].[RID]
FROM [dbo].[EmailRecipients] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM (
        SELECT DISTINCT [t11].[RID]
        FROM (
            SELECT [t10].[RID]
            FROM (
                SELECT DISTINCT [t4].[RID]
                FROM [dbo].[UserGroupMessageEmailDetail] AS [t1]
                INNER JOIN [dbo].[UserGroupMessages] AS [t2] ON [t2].[ID] = [t1].[GroupMessageID]
                INNER JOIN [dbo].[aspnet_Users] AS [t3] ON [t3].[UserId] = [t2].[UserId]
                INNER JOIN [dbo].[UserEmailRecipients] AS [t4] ON [t4].[RID] = [t1].[RID]
                WHERE ([t1].[GroupMessageID] = @p0) AND ([t3].[LoweredUserName] = LOWER(@p1))
                UNION
                SELECT DISTINCT [t9].[RID]
                FROM [dbo].[UserGroupMessageContactDetails] AS [t5]
                INNER JOIN [dbo].[UserGroupMessages] AS [t6] ON [t6].[ID] = [t5].[GroupMessageID]
                INNER JOIN [dbo].[aspnet_Users] AS [t7] ON [t7].[UserId] = [t6].[UserId]
                INNER JOIN [dbo].[UserContacts] AS [t8] ON [t8].[ContactID] = [t5].[ContactID]
                INNER JOIN [dbo].[UserEmailRecipients] AS [t9] ON [t9].[RID] = [t8].[ContactID]
                WHERE ([t7].[LoweredUserName] = @p2) AND ([t5].[GroupMessageID] = @p3) AND (NOT ([t9].[IsDeleted] = 1))
                ) AS [t10]
            ) AS [t11]
        ) AS [t12]
    WHERE [t12].[RID] = [t0].[RID]
    )
', '02-09-2015 06:14:15 AM'

我认为问题出现在第三个参数插入中。

如果需要更多信息,请告诉我......

0 个答案:

没有答案