c#linq在datatable上的字段值之前和之后添加单引号

时间:2021-06-03 20:55:38

i have data table. now, i am converting/export this data table to csv. for that my code is:

我有数据表。现在,我正在将此数据表转换/导出到csv。因为我的代码是:

var valueLines = dt.AsEnumerable()
                           .Select(row => string.Join(",", row.ItemArray));

Here, suppose my fields are **Name Number Rollname RollNumber**. after this query fire. it convert like: **Name,Number,Rollname,RollNumber**

在这里,假设我的字段是** Name Number Rollname RollNumber **。在此查询之后。它转换如:**名称,数字,Rollname,RollNumber **

it's work. i am getting csv file perfect. but issue this. suppose some filed value like: Roll,Number ...at that time. i am getting 2 fields/cell on csv....i am thinking to add ' before & after field values.

这是工作。我正在获得完美的csv文件。但发出这个。假设某些字段值如:Roll,Number ......那时候。我在csv上得到2个字段/单元格....我正在考虑添加'字段前后值'。

or any another way to skip issue?

或任何其他方式跳过问题?

3 个解决方案

#1


3  

CSV accepts items enclosed in double-quotes. You can put double-quotes around each item by modifying your code as follows:

CSV接受用双引号括起来的项目。您可以通过修改代码在每个项目周围加上双引号,如下所示:

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join(
        ","
    ,   row.ItemArray.Select(s => s.ToString().IndexOf(',') < 0 ? s : string.Format("\"{0}\"", s))));

The above replaces row.ItemArray with a Select that checks each item for ',', and puts double-quotes around strings that have commas.

上面用一个Select替换row.ItemArray,它将每个项目检查为',',并在带有逗号的字符串周围加上双引号。

#2


2  

Yes, one way would be wrap the fields in quotes on use a different delimiter that is rare like ^.

是的,一种方法是在引号中包含字段,使用不同的分隔符,如^。

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join(",", row.ItemArray
                                       .Select(f => string.Format("\"{0}\"", f))));

or

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join("^", row.ItemArray));

In general you should use a CSV-reader instead of reading the strings and splitting them manually. They support quoting characters and many more features. I can recommend this:

通常,您应该使用CSV阅读器而不是读取字符串并手动分割它们。它们支持引用字符和更多功能。我可以推荐这个:

http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

#3


0  

This is easy, remember you can mix old school concatenation with linq:

这很容易,记住你可以将旧学校连接与linq混合:

    var valueLine = (from row in dt.AsEnumerable()
                     select "'" + row.Field("field name") + "'").ToList();
    

#1


3  

CSV accepts items enclosed in double-quotes. You can put double-quotes around each item by modifying your code as follows:

CSV接受用双引号括起来的项目。您可以通过修改代码在每个项目周围加上双引号,如下所示:

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join(
        ","
    ,   row.ItemArray.Select(s => s.ToString().IndexOf(',') < 0 ? s : string.Format("\"{0}\"", s))));

The above replaces row.ItemArray with a Select that checks each item for ',', and puts double-quotes around strings that have commas.

上面用一个Select替换row.ItemArray,它将每个项目检查为',',并在带有逗号的字符串周围加上双引号。

#2


2  

Yes, one way would be wrap the fields in quotes on use a different delimiter that is rare like ^.

是的,一种方法是在引号中包含字段,使用不同的分隔符,如^。

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join(",", row.ItemArray
                                       .Select(f => string.Format("\"{0}\"", f))));

or

var valueLines = dt.AsEnumerable()
    .Select(row => string.Join("^", row.ItemArray));

In general you should use a CSV-reader instead of reading the strings and splitting them manually. They support quoting characters and many more features. I can recommend this:

通常,您应该使用CSV阅读器而不是读取字符串并手动分割它们。它们支持引用字符和更多功能。我可以推荐这个:

http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

#3


0  

This is easy, remember you can mix old school concatenation with linq:

这很容易,记住你可以将旧学校连接与linq混合:

    var valueLine = (from row in dt.AsEnumerable()
                     select "'" + row.Field("field name") + "'").ToList();