Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guid as Property value #19

Open
lau74 opened this issue Apr 17, 2019 · 4 comments
Open

Guid as Property value #19

lau74 opened this issue Apr 17, 2019 · 4 comments

Comments

@lau74
Copy link

lau74 commented Apr 17, 2019

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.DataExtractor1.d__8.MoveNext()

@ipvalverde
Copy link
Owner

ipvalverde commented Apr 17, 2019

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();

@lau74
Copy link
Author

lau74 commented Apr 18, 2019 via email

@ipvalverde ipvalverde self-assigned this Oct 15, 2019
@paradisehuman
Copy link

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!

@ipvalverde
Copy link
Owner

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;

@ipvalverde ipvalverde removed their assignment Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants