Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3696

Linq, Polimorphism, Downcast, OfType delivers wrong type

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.12.2, 2.12.3
    • Component/s: Linq
    • None
    • Environment:
      Windows 10, Visual Studio 2019

      Hello

      I have insert a Record of Type AlotOfDataEntry wich Inherited LessDataEntry. If i try to get LessDataEntry from the collection, all Data of AlotOfDataEntry will be loaded. It should only load the Data from LessDataEntry.

      ^^

      public class MongoConnector
         {
             public class BaseEntry
             {
                 public BaseEntry()
                 {
                     Id = Guid.NewGuid();
                 }
                 public Guid Id { get; set; }
             }
             public class AnotherEntry : BaseEntry
             {
                 public String Info { get; set; }
             }
             public class HelpEntry : BaseEntry
             {
                 public String Help { get; set; }
             }
             public class LessDataEntry : BaseEntry
             {
                 public String Name { get; set; }
             }
             public class AlotOfDataEntry : LessDataEntry
             {
                 public List<int> DetailData { get; set; }
             }
             public void Do()
             {
                 BsonClassMap.RegisterClassMap<BaseEntry>(cm =>
                 {
                     cm.AutoMap();
                     cm.SetIsRootClass(true);
                     cm.AddKnownType(typeof(LessDataEntry));
                     cm.AddKnownType(typeof(AlotOfDataEntry));
                     cm.AddKnownType(typeof(AnotherEntry));
                     cm.AddKnownType(typeof(HelpEntry));
                 });
                 AlotOfDataEntry aode = new AlotOfDataEntry() { Name = "AnyName",DetailData=new List<int>() };
       
                 for (int i = 0; i < 100000; i++)
                     aode.DetailData.Add(i);
       
                 var db = new MongoClient("mongodb://localhost/").GetDatabase("Test");
                 var collection = db.GetCollection<AlotOfDataEntry>("testCollection");
                 collection.InsertOne(aode);
       
                 // Get Data as LessDataEntry  will return AlotOfDataEntris
                 var collectionLess = db.GetCollection<LessDataEntry>("testCollection");
       
                 List<LessDataEntry> list = collectionLess.AsQueryable<LessDataEntry>().OfType<LessDataEntry>().ToList();
       
                 db.DropCollection("testCollection");
       
                 foreach (var entry in list)
                     System.Console.WriteLine(entry.GetType().FullName);
       
             }
         }

       

        1. Watch.PNG
          11 kB
          Thomas M

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            thomas.mamoser@stickfish.at Thomas M
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: