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

ToDataTable throws on type with public static member #102

Closed
GoogleCodeExporter opened this issue Aug 13, 2015 · 6 comments
Closed

ToDataTable throws on type with public static member #102

GoogleCodeExporter opened this issue Aug 13, 2015 · 6 comments

Comments

@GoogleCodeExporter
Copy link

When trying to convert to a data table a lisdt of System.Drawing.Point I get 
the following error "Static field requires null instance, non-static field 
requires non-null instance.Parameter name: expression". 

code to reproduce:
System.Collections.Generic.List<System.Drawing.Point> l = new 
System.Collections.Generic.List<System.Drawing.Point>();
l.Add(new System.Drawing.Point(1,2));
MoreLinq.MoreEnumerable.ToDataTable(l);

Original issue reported on code.google.com by AvniD...@gmail.com on 18 May 2015 at 3:40

@GoogleCodeExporter
Copy link
Author

This is indeed a bug but meanwhile you might want use the following as a 
workaround:

MoreLinq.MoreEnumerable.ToDataTable(l, p => p.X, p => p.Y);

Even once fixed, I think the above workaround will be the actual usage you want 
since Point has an instance property of IsEmpty (in addition to X and Y) that 
will get included as a column in the resulting DataTable.

Original comment by azizatif on 18 May 2015 at 4:04

  • Changed state: Accepted

@GoogleCodeExporter
Copy link
Author

(fixing issue summary)

Original comment by azizatif on 18 May 2015 at 4:41

  • Changed title: ToDataTable throws on type with public static member

@GoogleCodeExporter
Copy link
Author

The exception is thrown by Expression.Field and is a breaking change with .NET 
4.x. On .NET 3.5, there is no exception thrown and DataTable is created with 
static members also appearing as columns so perhaps this slipped unnoticed 
previously.

System.ArgumentException : Static field requires null instance, non-static 
field requires non-null instance.
Parameter name: expression
   at System.Linq.Expressions.Expression.Field(Expression expression, FieldInfo field)
   at System.Linq.Expressions.Expression.MakeMemberAccess(Expression expression, MemberInfo member)
   at MoreLinq.MoreEnumerable.CreateMemberAccessor(Expression parameter, MemberInfo member) in ToDataTable.cs: line 142

Original comment by azizatif on 18 May 2015 at 5:35

  • Changed state: Started
  • Added labels: Milestone-Release1.1-SP1, Priority-High
  • Removed labels: Priority-Medium

@GoogleCodeExporter
Copy link
Author

Few comments: 
1. If there is a chance for a bug that only I will fall on, my life story is 
that I will fall on it :-)
2. Indeed I could use your suggestion of specifying the parameters to render to 
the data table but in my case this was just an example. I have a much larger 
class that I need to render to a data table and if I add fields to the class I 
now have to remember to add them to the rendering list of fields. Obviously I 
would prefer to skip the need if possible.
3. on a more technical level, I have some old code that does about the same (I 
prefer to use yours). The code is working and did not fail were yours did. use 
it at your own will
        Public Shared Function ToDataTable(Of T)(ByVal data As IList(Of T)) As DataTable
            Dim properties As ComponentModel.PropertyDescriptorCollection = ComponentModel.TypeDescriptor.GetProperties(GetType(T))
            Dim table As New DataTable()
            For Each prop As ComponentModel.PropertyDescriptor In properties
                table.Columns.Add(prop.Name, If(Nullable.GetUnderlyingType(prop.PropertyType), prop.PropertyType))
            Next prop
            If data IsNot Nothing Then
                For Each item As T In data
                    If item IsNot Nothing Then
                        Dim row As DataRow = table.NewRow()
                        For Each prop As ComponentModel.PropertyDescriptor In properties
                            row(prop.Name) = If(prop.GetValue(item), DBNull.Value)
                        Next prop
                        table.Rows.Add(row)
                    End If
                Next item
            End If
            Return table
        End Function

Original comment by AvniD...@gmail.com on 18 May 2015 at 7:19

@GoogleCodeExporter
Copy link
Author

This issue was closed by revision 1f24f2a60a25

Original comment by azizatif on 19 May 2015 at 4:52

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

Published as v1.1.1 (aka SP1) as well as source packages:

* https://www.nuget.org/packages/morelinq/1.1.1
* 
https://www.nuget.org/packages/MoreLinq.Source.MoreEnumerable.ToDataTable/1.0.1
* https://www.nuget.org/packages/MoreLinq.Source.MoreEnumerable/1.2.1

Original comment by azizatif on 19 May 2015 at 6:05

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant