Cannot convert string to int in LINQ query from MongoDB

So, I am executing a SelectMany query to iterate through lists inside objects in a collection. Here is my request:

var collection = _database.GetCollection<VehicleDataUpload>(VehiclesCollection);
var aggregation = collection.AsQueryable()
    .SelectMany(v => v.VehicleEntries)
    .Where(i => Convert.ToInt32(i.PostFlashDTCs) > 0)
    .ToList();

However, every time I run this, I get the following error:

The first random error like "System.InvalidOperationException" occurred in the file MongoDB.Driver.dll

I thought the problem was with the conversion function, so I changed it to:

.Where(i => Convert.ToInt32("1") > 0)

And still great. My colleague said that he can strangle the conversion of string 0, but when I hard-written to "0", it still works. For some reason, it simply cannot convert the class field. I set the field to a string and even set the default value for it:

public class VehicleEntry
{
    [BsonElement("PostFlashDTCs")]
    [BsonDefaultValue("0")]
    public String PostFlashDTCs { get; set; }
}

What is the cause of an InvalidFormatException when reading from the object itself?

( where) , , . :

foreach (VehicleEntry vehicle in aggregation1)
{
    int result;
    if (!Int32.TryParse(vehicle.PostFlashDTCs, out result))
    {
        Console.WriteLine("Bad value!");
    }
}

2

, , 12 VehicleEntry. ( ) , PostFlashDTCs, , .

3

Convert.ToInt32 LINQ, , ? - , ? , , , .

+4
1

mongo linq, , Mongo. , , Convert.ToInt32.

.ToList() Mongo, linq, Convert.ToInt32 .ToList.

, (https://jira.mongodb.org/browse/CSHARP-900), ...

.Where(i => (int)i.PostFlashDTCs > 0)

.

+2

Source: https://habr.com/ru/post/1624481/


All Articles