Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ Expression ParseMemberAccess(Type type, Expression expression)
Expression ParseEnumerable(Expression instance, Type elementType, string methodName, int errorPos, Type type)
{
bool isQueryable = TypeHelper.FindGenericType(typeof(IQueryable<>), type) != null;
bool isDictionary = TypeHelper.FindGenericType(typeof(IDictionary<,>), type) != null;
bool isDictionary = TypeHelper.IsDictionary(type);

var oldParent = _parent;

Expand Down
20 changes: 17 additions & 3 deletions src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public static Type FindGenericType(Type generic, Type type)

if (generic.GetTypeInfo().IsInterface)
{
foreach (Type intfType in type.GetInterfaces())
foreach (Type interfaceType in type.GetInterfaces())
{
Type found = FindGenericType(generic, intfType);
if (found != null) return found;
Type found = FindGenericType(generic, interfaceType);
if (found != null)
{
return found;
}
}
}

Expand Down Expand Up @@ -440,5 +443,16 @@ public static object ParseEnum(string value, Type type)

return null;
}

public static bool IsDictionary(Type type)
{
return
FindGenericType(typeof(IDictionary<,>), type) != null ||
#if NET35 || NET40
false;
#else
FindGenericType(typeof(IReadOnlyDictionary<,>), type) != null;
#endif
}
}
}
57 changes: 39 additions & 18 deletions test/System.Linq.Dynamic.Core.Tests/MikArea/Dictionary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using FluentAssertions;
using NFluent;
Expand All @@ -13,6 +14,7 @@ public class Customer
{
public string City { get; set; }
public Dictionary<string, Order> Orders { get; set; }
public IReadOnlyDictionary<string, Order> ReadOnlyOrders { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
}
Expand All @@ -22,13 +24,32 @@ public class Order
}

[Fact]
public void Test_ContainsKey_1()
public void ReadOnlyDictionary_ContainsKey_1()
{
var customers = new List<Customer>()
var orders = new ReadOnlyDictionary<string, Order>(new Dictionary<string, Order>
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
{ "TEST", new Order() }
});
var customers = new List<Customer>
{
new Customer { City = "abc", CompanyName = "ZZZ", ReadOnlyOrders = orders }
};

var data = customers.AsQueryable()
.Where("ReadOnlyOrders.ContainsKey(\"TEST\")")
.ToList();

data.Should().HaveCount(1);
}

[Fact]
public void Dictionary_ContainsKey_1()
{
var customers = new List<Customer>
{
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));

Expand All @@ -41,13 +62,13 @@ public void Test_ContainsKey_1()
}

[Fact]
public void Test_ContainsKey_2()
public void Dictionary_ContainsKey_2()
{
var customers = new List<Customer>()
var customers = new List<Customer>
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));

Expand All @@ -61,13 +82,13 @@ public void Test_ContainsKey_2()
}

[Fact]
public void Test_ContainsKey_3()
public void Dictionary_ContainsKey_3()
{
var customers = new List<Customer>()
var customers = new List<Customer>
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST1", new Order()));
customers.ForEach(x => x.Orders.Add(x.City + "TEST2", new Order()));
Expand All @@ -91,7 +112,7 @@ public void Test_ContainsKey_3()
#else
[Fact(Skip = "Fails in NET452 CI")]
#endif
public void Test_DynamicIndexCall() // https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/397
public void DynamicIndexCall() // https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/397
{
object CreateDicParameter(string name) => new Dictionary<string, object>
{
Expand Down