Skip to content

Commit 77da1b3

Browse files
committed
CSHARP-4592: Support indexer for IDictionary (as well as Dictionary).
1 parent 676d6b9 commit 77da1b3

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/TypeExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ public static bool IsValueTuple(this Type type)
216216

217217
public static bool TryGetIDictionaryGenericInterface(this Type type, out Type idictionaryGenericInterface)
218218
{
219+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))
220+
{
221+
idictionaryGenericInterface = type;
222+
return true;
223+
}
224+
219225
foreach (var interfaceType in type.GetInterfaces())
220226
{
221227
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IDictionary<,>))
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using FluentAssertions;
19+
using MongoDB.Driver.Linq;
20+
using MongoDB.TestHelpers.XunitExtensions;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
24+
{
25+
public class CSharp4592Tests : Linq3IntegrationTest
26+
{
27+
[Theory]
28+
[ParameterAttributeData]
29+
public void Project_dictionary_value_should_work(
30+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
31+
{
32+
var collection = CreateCollection(linqProvider);
33+
34+
var queryable = collection.AsQueryable()
35+
.Select(x => x.Dict["test"]);
36+
37+
var stages = Translate(collection, queryable);
38+
if (linqProvider == LinqProvider.V2)
39+
{
40+
AssertStages(stages, "{ $project : { test : '$Dict.test', _id : 0 } }"); // LINQ2 translates slightly differently
41+
}
42+
else
43+
{
44+
AssertStages(stages, "{ $project : { _v : '$Dict.test', _id : 0 } }");
45+
}
46+
47+
var results = queryable.ToList();
48+
results.Should().Equal(3, 0); // missing values deserialize as 0
49+
}
50+
51+
private IMongoCollection<ExampleClass> CreateCollection(LinqProvider linqProvider)
52+
{
53+
var collection = GetCollection<ExampleClass>("test", linqProvider);
54+
CreateCollection(
55+
collection,
56+
new ExampleClass { Id = 1, Dict = new Dictionary<string, int> { ["test"] = 3 } },
57+
new ExampleClass { Id = 2, Dict = new Dictionary<string, int> { ["a"] = 4 } });
58+
return collection;
59+
}
60+
61+
private class ExampleClass
62+
{
63+
public int Id { get; set; }
64+
public IDictionary<string, int> Dict { get; set; }
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)