-
Type:
Bug
-
Resolution: Done
-
Priority:
Minor - P4
-
Affects Version/s: 2.2
When using: yourMongoCollection.AsQueryable().Select(projectionExpression).ToList() in versions greater than 2.1.X it fails.
The examples below just call Count() for demonstration purposes.
internal class Program
{
internal class Person
{
public Guid Id { get; set; }
public string Name { get; set; }
}
private static void Main(string[] args)
{
// RegisterConventions
var conventionPack = new ConventionPack
{
new IgnoreIfNullConvention(true),
new NoIdMemberConvention()
};
ConventionRegistry.Register("GlobalConventions", conventionPack, t => true);
// Mapping People collection
var map = BsonClassMap.RegisterClassMap<Person>();
map.AutoMap();
map.MapProperty(c => c.Id).SetIsRequired(true);
map.SetIdMember(map.GetMemberMap(c => c.Id));
// Creating mongo client and dropping collection
var mongoClient = new MongoClient("mongodb://buildbee");
var mongoDatabase = mongoClient.GetDatabase("db");
mongoDatabase.DropCollectionAsync("people").Wait();
var peopleCollection = mongoDatabase.GetCollection<Person>("people");
// one document is inserted
peopleCollection.InsertOneAsync(new Person
{
Id = Guid.NewGuid(),
Name = "A"
}).Wait();
DoIt("1- Count", () => peopleCollection.AsQueryable().Count());
DoIt("2- ToList Without Select", () => peopleCollection.AsQueryable().ToList().Count);
DoIt("3.1- ToList With Select", () => peopleCollection.AsQueryable().Select(p => p).ToList().Count);
DoIt("3.2- ToList With Select", () => peopleCollection.AsQueryable().Select(p => new Person
{
Id = p.Id,
Name = p.Name,
}).ToList().Count);
DoIt("3.3- ToList With Select", () => peopleCollection.AsQueryable().Select(p => new Person
{
Id = p.Id,
// Name = p.Name,
}).ToList().Count);
DoIt("3.4- ToList With Select", () => peopleCollection.AsQueryable().Select(p => new Person
{
// Id = p.Id,
Name = p.Name,
}).ToList().Count);
}
private static void DoIt(string message, Func<int> countQuery)
{
try
{
Console.WriteLine(message);
Console.WriteLine(countQuery());
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine();
}
}
1- Count
1
2- ToList Without Select
1
3.1- ToList With Select
1
3.2- ToList With Select
1
3.3- ToList With Select
1
3.4- ToList With Select
1
Press any key to continue . . .
1- Count
1
2- ToList Without Select
1
3.1- ToList With Select
1
3.2- ToList With Select
System.FormatException: Element 'Id' does not match any field or property of class ConsoleApplication1.Program+Person.
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ...
3.3- ToList With Select
System.FormatException: Element 'Id' does not match any field or property of class ConsoleApplication1.Program+Person.
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ...
3.4- ToList With Select
System.FormatException: Required element '_id' for property 'Id' of class ConsoleApplication1.Program+Person is missing.
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ...
Press any key to continue . . .
- is duplicated by
-
CSHARP-1606 Error using Select function
-
- Closed
-
- is related to
-
CSHARP-1719 Selecting from a IMongoQueryable collection with a Linq Expression throws an error because of 'Id' property
-
- Closed
-
-
CSHARP-1745 Deserialization issue when using GroupBy
-
- Closed
-