0

Getting a System.InvalidCastException when trying to save custom classes into a LiteDB collection.

The line of code where the error is thrown:

using (var db = new LiteDatabase(_datastore))
    {
        var results = db.GetCollection<ImageResult>("ImageResults");
        ImageResult result;
        if (results.Exists(x => x.ImageName == Path.GetFileName(_currentFile)))
        {
            result = results.Find(x => x.ImageName == Path.GetFileName(_currentFile)).FirstOrDefault();
        }
        else
        {
            result = new ImageResult {_id = new Guid(), ImageName = Path.GetFileName(_currentFile)};
        }
    }

I have also tried using results.FindOne() and using the condition ? consequent : alternative format to the query.

I'm not sure what is causing the issue within my class. I am using .NETFramework Version=v4.6.1 in a WinForms project.

The classes are as follows and are saved in their own CustomClasses.cs:

{
    public class SpatialSet
    {
        public Guid _id { get; set; }
        public String Name { get; set; }
        public FeatureType Type { get; set; }
        public List<List<PointF>> Points { get; set; }
        public Shapefile GenerateShapefile()
        {
            var s = new Shapefile{Name = Name, FeatureType =  Type};
            foreach (var pList in Points)
            {
                var c = pList.Select(x => new Coordinate(x.X, x.Y));
                s.Features.Add(c,Type);
            }

            return s;
        }

        public void FromShapefile(Shapefile shapefile)
        {
            _id = Guid.NewGuid();
            Name = shapefile.Name;
            Type = shapefile.FeatureType;
            var l = new List<List<PointF>>();
            foreach (var f in shapefile.Features)
            {
                var cLists = f.Coordinates;
                var pList = new List<PointF>() ;

                foreach (var coordinate in cLists)
                {
                    var p = new PointF((float)coordinate.X,(float)coordinate.Y);
                    pList.Add(p);
                }
                l.Add(pList);
            }
            Points = l;
        }

    }
    public  class ImageResult
    {
        public Guid _id { get; set; }
        public string ImageName { get; set; }
        public SpatialSet Points { get; set; }
        public SpatialSet Polygons { get; set; }
        public SpatialSet Circles { get; set; }

    }
}

Details of System.InvalidCastException error message:

System.InvalidCastException: Unable to cast object of type 'ImageManipulation.SpatialSet' to type 'System.Collections.IEnumerable'.
  at at LiteDB.BsonMapper.DeserializeList(Type type, BsonArray value)
  at at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
  at at LiteDB.BsonMapper.DeserializeObject(EntityMapper entity, Object obj, BsonDocument value)
  at at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
  at at LiteDB.LiteQueryable`1.<ToEnumerable>b__27_2(BsonDocument x)
  at at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  at at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
  at ImageManipulation.FrmMain.btnSaved_Click(Object sender, EventArgs e) in C:\Users\michael.thompson\RiderProjects\ImageManipulation\ImageManipulation\FrmMain.cs:337
  at at System.Windows.Forms.Control.OnClick(EventArgs e)
  at at System.Windows.Forms.Button.OnClick(EventArgs e)
  at at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  at at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  at at System.Windows.Forms.Control.WndProc(Message& m)
  at at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  at at System.Windows.Forms.Button.WndProc(Message& m)
  at at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  at at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  at at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  at at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  at at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  at at System.Windows.Forms.Application.Run(Form mainForm)
  at ImageManipulation.Program.Main() in C:\Users\michael.thompson\RiderProjects\ImageManipulation\ImageManipulation\Program.cs:16
MBT86
  • 51
  • 4

1 Answers1

0

I ran your example (a simplified version of it, because you didn't provide all the classes) and could not reproduce the issue. Maybe you could provide all the classes and I'll try to run the complete example.

  • Will need to have the _DotSpatial.Data [link] (https://www.nuget.org/packages/DotSpatial.Data/), for the other classes. I've noticed that it is using DotSpatial objects cause the failure. Specifically `public FeatureType Type`. I have swapped to the following: `public string Type { get; set; }` within the `FromShapefile()` `Type = shapefile.FeatureType.ToString();` and within the `GenerateShapefile()` `Enum.TryParse(Type, true, out FeatureType ft); var s = new Shapefile{Name = Name, FeatureType = ft};` This works but how do i use the DotSpatial objects in my classes? – MBT86 Apr 16 '20 at 11:42