从数据表中的所有单元格中删除单引号-创建新表-在Vb.Net中使用LINQ [英] Remove Single Quotes From All Cells in a DataTable - Creating New Table - Using LINQ in Vb.Net

查看:31
本文介绍了从数据表中的所有单元格中删除单引号-创建新表-在Vb.Net中使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,表中有很多值,它们周围可能有单引号.我需要创建一个没有那些单引号的重复表.我无法修改原始表,因为以后需要按原样使用它.

I have a table with many values inside of it that might have single quotes around them. I need to create a duplicate table without those single quotes. I can't modify the original table as I need to use it, 'as-is' later.

我已经完成了一个table.Clone并使用foreach遍历行和列,并根据需要删除单引号,然后将新行放入新表中.它有效并且很直接.....但是

I've done this with a table.Clone and using a foreach to loop through the rows and columns removing single quotes as I need and then putting that new row into the new table. It works and it's straight forward.....but

我想用LINQ做同样的事情.这是我的尝试.

I'd like to do the same thing using LINQ. This was my attempt....

        Dim myResults = From myRow In dtTable _
                    From myItem In myRow.ItemArray _
                    Where TypeOf myItem Is String AndAlso _
                    myItem.ToString.StartsWith("'"c) AndAlso _
                    myItem.ToString.EndsWith("'"c)

如您所见-我距离不远.我很难找到没有在DataRow中查看特定列的示例.看来我的代码确实拉回了所有匹配的结果-但是我迷失了如何创建重复表/修改值的方法?

As you can see - I didn't get very far. I had trouble finding examples that weren't looking at a specific column in the DataRow. It looks like my code does pull back all the matching results - but I'm at a lose for how I can create a duplicate table/modify the values?

编辑-我已经为此悬赏,希望有人可以提供解决方案.唯一的要求是不要使用For Each;因为我已经知道该怎么做.我已经走近了-但我似乎仍然无法创建新行或新表.

EDIT - I've started a bounty for this in the hopes that someone can provide a solution. The only requirement is to not using a For Each; as I already know how to do that. I've gotten closer - but I still can't seem to create a new row or a new table.

当我尝试这样做时,我的新方法陷入了僵局:

My new approach hits a dead-end when I try to do this:

Dim MyNewRow As New Data.DataRow With {.ItemArray = myRemovedQuotes.ToArray}

我收到的错误消息说,在此情况下无法访问错误1'System.Data.DataRow.Protected Friend Sub New(builder作为System.Data.DataRowBuilder)',因为它是'Protected Friend'."

The Error message I get says, "Error 1 'System.Data.DataRow.Protected Friend Sub New(builder As System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected Friend'."

推荐答案

如果您真的必须使用LINQ(也许是一项学习练习),那么也许可以使用以下内容(例如C#;我的VB)恐怕离翻译还很遥远):

If you really must use LINQ (as a learning exercise, perhaps), then maybe something like below (as C#; my VB is nowhere near up to translating it, I'm afraid):

        DataTable clone = original.Clone();
        string t;
        var qry = from DataRow row in original.Rows
                  let arr = row.ItemArray
                  select Array.ConvertAll(arr, s =>
                      (t = s as string) != null
                      && t.StartsWith("\'")
                      && t.EndsWith("\'") ? t.Trim('\'') : s);
        foreach (object[] arr in qry) {
            clone.Rows.Add(arr);
        }

问题是 ItemArray 需要扩展,同时保留单独的行(因此, SelectMany 不能选择); Array.ConvertAll 似乎是最简单的方法.

The problem is that the ItemArray needs to be expanded while retaining the separate rows (so SelectMany isn't an option); Array.ConvertAll seems the easiest way.

这篇关于从数据表中的所有单元格中删除单引号-创建新表-在Vb.Net中使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆