Replies: 20 comments 1 reply
-
You would at least have to project the |
Beta Was this translation helpful? Give feedback.
-
What do you mean by project the IQueryable? Do you mean, looping though it, replacing it? |
Beta Was this translation helpful? Give feedback.
-
Actually, what you're doing should work since What do you mean by illegal characters? When writing fields that contain those characters, they'll be escaped. Also, if you're doing |
Beta Was this translation helpful? Give feedback.
-
Thx for the header duplication tips, I would avoid using WriteHeader. Regarding the illegal chars, in one of the fields in "query" I have like this sentence: "This is a sentence with \n illegal ;;;; chars". I would just like to replace the \n and ;;;; by nothing. But if they are really escaped that's fine for me. Should I put something else in the csv.configuration or will it do this anyway? |
Beta Was this translation helpful? Give feedback.
-
Have you tried it? The CSV format allow for delimiters and newlines to be contained within fields by escaping the fields. CsvHelper will do everything for you. This is the default, so you can remove if i you want. csv.Configuration.HasHeaderRecord = true; When the code exits the sw.Flush(); |
Beta Was this translation helpful? Give feedback.
-
I faced a similar issue. I need to get rid of some symbols from each column in a row in a process of writing of csv string but I can't find such feature in |
Beta Was this translation helpful? Give feedback.
-
I tried to override method
|
Beta Was this translation helpful? Give feedback.
-
@Imishin If you need to remove characters, whey don't you do it before sending it through CsvHelper? |
Beta Was this translation helpful? Give feedback.
-
@JoshClose Because I already have this data in an object, the object contains a lot of properties so in an output I have about 300 fields per row, therefore, it's too complicated and not elegant to change manually the object data in the beginning and the value for each of the fields in the end. So I only try to map this object into a csv string hoping that it's possible to change it on the fly. |
Beta Was this translation helpful? Give feedback.
-
You can use a custom type converter to do this. void Main()
{
var records = new List<Foo>
{
new Foo { Id = 1, Name = "one;two" },
new Foo { Id = 2, Name = "three\nfour" },
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<FooMap>();
csv.WriteRecords(records);
writer.ToString().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.Id).TypeConverter<ReplaceConverter>();
Map(m => m.Name).TypeConverter<ReplaceConverter>();
}
}
public class ReplaceConverter : DefaultTypeConverter
{
public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
{
var converter = row.Configuration.TypeConverterCache.GetConverter(memberMapData.Member.GetType());
var s = converter.ConvertToString(value, row, memberMapData);
s = Regex.Replace(s, "[\n;]", string.Empty);
return s;
}
} |
Beta Was this translation helpful? Give feedback.
-
I got it, but I don't wanna replicate the applying of this converter for each field/property of the class:
Because in a case of multiple fields/properties I have to apply "copy-paste" practice :( |
Beta Was this translation helpful? Give feedback.
-
Could you help me to override the method |
Beta Was this translation helpful? Give feedback.
-
You do the same thing as you would in a type converter. public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.Id).ConvertUsing((field) =>
{
// Convert to string however you like. This is using
// .NET's built in type conversion system.
var converter = TypeDescriptor.GetConverter(typeof(int));
var s = converter.ConvertToString(field.Id);
s = Regex.Replace(s, "[\n;]", string.Empty);
return s;
});
Map(m => m.Name).ConvertUsing((Foo field) =>
{
// Convert to string however you like. This is using
// .NET's built in type conversion system.
var converter = TypeDescriptor.GetConverter(typeof(string));
var s = converter.ConvertToString(field.Name);
s = Regex.Replace(s, "[\n;]", string.Empty);
return s;
});
}
} |
Beta Was this translation helpful? Give feedback.
-
I fear I'm in the same boat as @Imishin ... I have a dozen or so (and counting...) classes that are exported. They all descend from a generic
Now... I have been instructed to not quote any of the fields. And my counterpart doesn't expect newlines in the non-quotable string fields. A dynamic map has served me very well so far. It makes it very easy to add more DTOs in the future. Assigning a converter to each field looks awkward. Can I have a global converter for the string type? |
Beta Was this translation helpful? Give feedback.
-
This will replace the current csv.Configuration.TypeConverterCache.AddConverter<string>(new MyStringConverter()); |
Beta Was this translation helpful? Give feedback.
-
Ah, I struggled a bit until I noticed ConvertToString() was the one I was supposed to override. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi Josh, I have the same thing here. How do use the globally replaced converter? I add to my config...config.TypeConverterCache.AddConverter(new RemoveControlCharacterStringConverter()); but what change do I need to do in my class mapping? Thanks David |
Beta Was this translation helpful? Give feedback.
-
@davidcassell You shouldn't have to make a change in your mapping. If you're specifying a type converter on a property in your mapping though, then that type convertible will take precedence. Since you're adding it as a converter for a |
Beta Was this translation helpful? Give feedback.
-
@davidcassell One thing I've found is the Converter has to be added to the TypeConverterCache before the mapping is added. |
Beta Was this translation helpful? Give feedback.
-
Thanks guys. it works well |
Beta Was this translation helpful? Give feedback.
-
In my IQueryable I have some illegal characters, for instance, '\n' and ';' which I would like to replace. However, in the way I did it I can't do it as I'm writting the query directly
csv.WriteRecords(query)
. How could I do that?Beta Was this translation helpful? Give feedback.
All reactions