如何在ado.net中编写包含数据字典的查询

时间:2019-07-11 12:06:29

标签: c# linq ado.net sqlcommand data-dictionary

我想编写使用linq和数据字典编写的查询,我想将该查询转换为ado.net形式

/// <summary>
/// Method called when a new dialog has been pushed onto the stack and is being activated.
/// </summary>
/// <param name="dc">The dialog context for the current turn of conversation.</param>
/// <param name="options">(Optional) additional argument(s) to pass to the dialog being started.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>If the task is successful, the result indicates whether the dialog is still
/// active after the turn has been processed by the dialog.</remarks>
public abstract Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken));

以上是我想将其转换为ado.net形式以提供这种类型的字典的查询

  

ToDictionary(key => key.Name??t.TName,Val => Val.Value);

我已经写了一半的代码,直到填充数据集为止 但是进一步,我很困惑如何进一步将其转换为具有上述密钥对值的字典。

这是我尝试过的代码

var insData = new Dictionary<string, string>();

                    insData = db.Query<TableName>("select Name, Value from Table where PId={id} and type<>{(int)Enum.PElement}").ToDictionary(key => key.Name??t.TName, Val => Val.Value);

但是我遇到了类似的错误

  

键对值不包含Name的定义,并且没有可访问的>扩展方法Name接受KeyValuepair类型的第一个参数

我想在ado.net中处理该查询,就像在上面显示的linq中一样。

1 个答案:

答案 0 :(得分:1)

我会做这样的事情:

        IEnumerable<DataRow> dataRows;
        using (var dataSet = new DataSet())
        {
            using (var command = new SqlCommand($"select Name, Value from Table where PId={id} and type<>{(int) Enum.PElement}", con))
            {
                using (var sda2 = new SqlDataAdapter(command))
                {
                    sda2.Fill(dataSet);
                }
            }

            dataRows = dataSet.Tables[0].Rows.OfType<DataRow>();
        }

        return dataRows.ToDictionary(row => row["Name"].ToString(), row => row["Value"].ToString());