EPPlus.DataExtractor
EPPlus.DataExtractor copied to clipboard
Guid as Property value
Can the POCO have a Guid or as data type?
I am getting an error because a Guid can be parse but not casted.
System.InvalidCastException : Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at OfficeOpenXml.Utils.ConvertUtil.GetTypedCellValue[T](Object value)
at EPPlus.DataExtractor.PropertyValueSetter2.SetPropertyValue(TModel dataInstance, ExcelRangeBase cell) at EPPlus.DataExtractor.DataExtractor
1.<GetData>d__8.MoveNext()
Yes, this should be possible. I didn't test it though it might be related to the default converter not being able to convert from string to Guid. Are you sure that the cell value can be parsed to a Guid?
For now, you can provide a custom function to the WithProperty
where you can perform the parsing yourself. The parameter will be an "object" with the content of the cell and the return will be mapped to the property that you indicated, something like this:
var data = package.Workbook.Worksheets["MyWorksheet"]
.Extract<MyPoco>()
.WithProperty(p => p.MyGuidProperty, "A",
(value) =>
{
Guid convertedValue;
if (!Guid.TryParse(value.ToString(), out convertedValue))
throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}",
value.GetType(), value.ToString()));
return convertedValue;
})
.GetData(1, 100)
.ToList();
Thanks for your quick reply. Yes i can, I use the setPropertyCastedValueCallback on the string column and try parse the cell value and it passes as a Guid. But i am going to use the snippet you send me because that looks like a better solution so my POCO can have the Guid property.
On Wed, Apr 17, 2019 at 3:12 PM Israel Valverde [email protected] wrote:
Yes, this should be possible. I didn't test it thought it might be related to the default converter not being able to convert from string to Guid. Are you sure that the cell value can be parsed to a Guid?
For now, you can provide a custom function to the https://github.com/ipvalverde/EPPlus.DataExtractor/blob/f652aa54a94b0bc5ed7e284dc6fbc4bd4e09909c/src/EPPlus.DataExtractor/DataExtractor.cs#L74 WithProperty where you can perform the parsing yourself. The parameter will be an "object" with the content of the cell and the return will be mapped to the property that you indicated, something like this:
var data = package.Workbook.Worksheets["MyWorksheet"] .Extract<MyPoco>() .WithProperty(p => p.MyGuidProperty, "A", (value) => { Guid convertedValue; if (!Guid.TryParse(value.ToString(), out convertedValue)) throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}", value.GetType(), value.ToString()));
return convertedValue; }) .GetData(1, 100) .ToList();
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ipvalverde/EPPlus.DataExtractor/issues/19#issuecomment-484280774, or mute the thread https://github.com/notifications/unsubscribe-auth/AL3NCG6KZG75XGOHBTGI2D3PQ6OFJANCNFSM4HGWQO6A .
Yes, this should be possible. I didn't test it though it might be related to the default converter not being able to convert from string to Guid. Are you sure that the cell value can be parsed to a Guid?
For now, you can provide a custom function to the
WithProperty
where you can perform the parsing yourself. The parameter will be an "object" with the content of the cell and the return will be mapped to the property that you indicated, something like this:var data = package.Workbook.Worksheets["MyWorksheet"] .Extract<MyPoco>() .WithProperty(p => p.MyGuidProperty, "A", (value) => { Guid convertedValue; if (!Guid.TryParse(value.ToString(), out convertedValue)) throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}", value.GetType(), value.ToString())); return convertedValue; }) .GetData(1, 100) .ToList();
@ipvalverde Hello. Value is null, and I'm got NullRefrenceException!
Yes, this should be possible. I didn't test it though it might be related to the default converter not being able to convert from string to Guid. Are you sure that the cell value can be parsed to a Guid? For now, you can provide a custom function to the
WithProperty
where you can perform the parsing yourself. The parameter will be an "object" with the content of the cell and the return will be mapped to the property that you indicated, something like this:var data = package.Workbook.Worksheets["MyWorksheet"] .Extract<MyPoco>() .WithProperty(p => p.MyGuidProperty, "A", (value) => { Guid convertedValue; if (!Guid.TryParse(value.ToString(), out convertedValue)) throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}", value.GetType(), value.ToString())); return convertedValue; }) .GetData(1, 100) .ToList();
@ipvalverde Hello. Value is null, and I'm got NullRefrenceException!
Hi @paradisehuman ,
If the value
can be null you should add a check for the null case, otherwise the statement value.ToString()
will throw an exception.
If your value can be null your model's Guid
property should be nullable ( public Guid? MyGuidProperty { get; set; }
). Then on the first line of the lambda function there could be a check like if (value == null) return null;