Exception: Unable to cast object while getting collections
See original GitHub issueHi 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(?):
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:
This problem is new to me. Maybe you guys have a solution because it is already working with other classes.
Greetings
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Great, glad to hear that. I gonna implement it in other classes meanwhile until the release. And again, good job with this DB.
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. 🙂