question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception: Unable to cast object while getting collections

See original GitHub issue

Hi guys,

I am really new to this database but I also really like it so far. BTW while implementing LiteDB to our project, the following issue came up.

Lets say we have a struct called Point2D. It has some internal variables which doesn’t matter here, but also 2 public variables called “X” and “Y”. We are using no ID here because we cannot change the struct (third-party). => Auto-ID

Something like:

public struct Point2D 
{
   public int X;
   public int Y;
}

I am filling the table/collection in this way (as an example), which is working fine so far:

using (var db = Database.Instance)
      {
        var results = db.GetCollection<Point2D>("mytable");

        foreach (var item in collection)
        {
          Point2D pnt = new Point2D(item.x, item.y);
          results.Insert(pnt);
        }
      }

After connecting to the DB using a LiteDBViewer/Studio here is the expected output(?): grafik

Now we are going to programmically query this to print out the Content:

try
      {
        using (var db = Database.Instance)
        {
          var results = db.GetCollection<Point2D>("mytable");

          foreach (var result in results.FindAll())
          {
            //Point2D pnt = new Point2D(result.X, result.Y);  METHODE 1
            //Console.WriteLine(pnt.ToString()); METHODE 1
            Console.WriteLine(result.ToString()); //METHODE 2 Because result is already type of Point2D
          }
        }
      }
      catch (LiteException ex)
      {
        throw ex;
      }

Now I got an exception because of this line:

var results = db.GetCollection<Point2D>("mytable");

Here is the stacktrace: grafik

This problem is new to me. Maybe you guys have a solution because it is already working with other classes.

Greetings

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
3HMonkeycommented, Jan 21, 2020

Hi @3HMonkey, no, only in master branch. My idea is release next nuget only in jan/31 with final v5 release.

Great, glad to hear that. I gonna implement it in other classes meanwhile until the release. And again, good job with this DB.

0reactions
JensSchadroncommented, Jan 22, 2020

Given that the issue has already been resolved in the master branch and it will be released in the next NuGet release, I’ll close the ticket. Feel free to reopen the ticket if you still have question regarding this ticket. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I cast an object to collection in Java?
1 Answer. or use a third party lib like guava collections. Then you should use that collection as an argument.
Read more >
InvalidCastException: Unable to cast object of type 'System ...
InvalidCastException : Unable to cast object of type 'System.Collections.ObjectModel.Collection 1[T]' to type 'System.Collections.Generic.
Read more >
Unable to cast object of type ObservableCollection
Unable to cast object of type ObservableCollection. I break out some Silverlight coding from time to time, to keep the rust off. And...
Read more >
System.InvalidCastException: Unable to cast object of type ...
Hi Again so this i a method where i get user id and there are two approaches to it one is that i...
Read more >
System.InvalidCastException error - Studio
Message: Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.Collections.Generic.IEnumerable`1[System.Text.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found