Skip to content

Commit bd28b1d

Browse files
committed
Support more comparisons with strings (#38)
1 parent f60437e commit bd28b1d

File tree

6 files changed

+83
-34
lines changed

6 files changed

+83
-34
lines changed

src/System.Linq.Dynamic.Core.SL50/System.Linq.Dynamic.Core.SL50.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
<Compile Include="..\System.Linq.Dynamic.Core\Compatibility\TypeBuilderExtensions.cs">
8080
<Link>Compatibility\TypeBuilderExtensions.cs</Link>
8181
</Compile>
82+
<Compile Include="..\System.Linq.Dynamic.Core\Compatibility\TypeConverterExtensions.cs">
83+
<Link>Compatibility\TypeConverterExtensions.cs</Link>
84+
</Compile>
8285
<Compile Include="..\System.Linq.Dynamic.Core\CustomTypeProviders\DefaultDynamicLinqCustomTypeProvider.cs">
8386
<Link>CustomTypeProviders\DefaultDynamicLinqCustomTypeProvider.cs</Link>
8487
</Compile>
@@ -136,6 +139,9 @@
136139
<Compile Include="..\System.Linq.Dynamic.Core\Res.cs">
137140
<Link>Res.cs</Link>
138141
</Compile>
142+
<Compile Include="..\System.Linq.Dynamic.Core\TypeConverterFactory.cs">
143+
<Link>TypeConverterFactory.cs</Link>
144+
</Compile>
139145
<Compile Include="..\System.Linq.Dynamic.Core\Validation\Check.cs">
140146
<Link>Validation\Check.cs</Link>
141147
</Compile>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+

2+
namespace System.ComponentModel
3+
{
4+
#if SILVERLIGHT
5+
internal static class TypeConverterExtensions
6+
{
7+
/// <summary>
8+
/// Converts the given string to the type of this converter.
9+
/// </summary>
10+
/// <param name="typeConverter">The System.ComponentModel.TypeConverter</param>
11+
/// <param name="text">The System.String to convert</param>
12+
/// <returns>An System.Object that represents the converted text.</returns>
13+
public static object ConvertFromInvariantString(this TypeConverter typeConverter, string text)
14+
{
15+
return typeConverter.ConvertFromString(text);
16+
}
17+
}
18+
#endif
19+
}

src/System.Linq.Dynamic.Core/ExpressionParser.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,7 @@ Expression ParseComparison()
635635
_token.id == TokenId.LessThan || _token.id == TokenId.LessThanEqual)
636636
{
637637
ConstantExpression constantExpr;
638-
#if !SILVERLIGHT
639638
TypeConverter typeConverter;
640-
#endif
641639
Token op = _token;
642640
NextToken();
643641
Expression right = ParseShift();
@@ -705,16 +703,14 @@ Expression ParseComparison()
705703
}
706704
}
707705
}
708-
#if !SILVERLIGHT
709-
else if ((constantExpr = right as ConstantExpression) != null && constantExpr.Value is string && (typeConverter = TypeDescriptor.GetConverter(left.Type)) != null)
706+
else if ((constantExpr = right as ConstantExpression) != null && constantExpr.Value is string && (typeConverter = TypeConverterFactory.GetConverter(left.Type)) != null)
710707
{
711708
right = Expression.Constant(typeConverter.ConvertFromInvariantString((string)constantExpr.Value), left.Type);
712709
}
713-
else if ((constantExpr = left as ConstantExpression) != null && constantExpr.Value is string && (typeConverter = TypeDescriptor.GetConverter(right.Type)) != null)
710+
else if ((constantExpr = left as ConstantExpression) != null && constantExpr.Value is string && (typeConverter = TypeConverterFactory.GetConverter(right.Type)) != null)
714711
{
715712
left = Expression.Constant(typeConverter.ConvertFromInvariantString((string)constantExpr.Value), right.Type);
716713
}
717-
#endif
718714
else
719715
{
720716
CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using JetBrains.Annotations;
2+
using System.ComponentModel;
3+
using System.Linq.Dynamic.Core.Validation;
4+
5+
namespace System.Linq.Dynamic.Core
6+
{
7+
internal static class TypeConverterFactory
8+
{
9+
/// <summary>
10+
/// Returns a type converter for the specified type.
11+
/// </summary>
12+
/// <param name="type">The System.Type of the target component.</param>
13+
/// <returns>A System.ComponentModel.TypeConverter for the specified type.</returns>
14+
public static TypeConverter GetConverter([NotNull] Type type)
15+
{
16+
Check.NotNull(type, nameof(type));
17+
18+
#if !SILVERLIGHT
19+
return TypeDescriptor.GetConverter(type);
20+
#else
21+
var attributes = type.GetCustomAttributes(typeof(TypeConverterAttribute), false);
22+
23+
if (attributes.Length != 1)
24+
return new TypeConverter();
25+
26+
var converterAttribute = (TypeConverterAttribute)attributes[0];
27+
var converterType = Type.GetType(converterAttribute.ConverterTypeName);
28+
29+
if (converterType == null)
30+
return new TypeConverter();
31+
32+
return Activator.CreateInstance(converterType) as TypeConverter;
33+
#endif
34+
}
35+
}
36+
}

src/System.Linq.Dynamic.Core/project.json

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.5.2",
2+
"version": "1.0.5.3",
33
"title": "System.Linq.Dynamic.Core",
44
"description": "This is a .NET Core port of the the Microsoft assembly for the .Net 4.0 Dynamic language functionality.",
55
"authors": [ "Microsoft", "Scott Guthrie", "King Wilder", "Nathan Arnott", "Stef Heyenrath" ],
@@ -14,7 +14,7 @@
1414
},
1515
"projectUrl": "https://github.com/StefH/System.Linq.Dynamic.Core",
1616
"licenseUrl": "https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/licence.txt",
17-
"releaseNotes": "Support strings as Enum Parameter Objects"
17+
"releaseNotes": "Support more comparisons with strings"
1818
},
1919

2020
"buildOptions": {
@@ -42,44 +42,26 @@
4242
},
4343
"net45": {
4444
"frameworkAssemblies": {
45-
},
46-
"dependencies": {
47-
"System.ComponentModel.TypeConverter": "4.1.0"
4845
}
4946
},
5047
"net451": {
5148
"frameworkAssemblies": {
52-
},
53-
"dependencies": {
54-
"System.ComponentModel.TypeConverter": "4.1.0"
5549
}
5650
},
5751
"net452": {
5852
"frameworkAssemblies": {
59-
},
60-
"dependencies": {
61-
"System.ComponentModel.TypeConverter": "4.1.0"
6253
}
6354
},
6455
"net46": {
6556
"frameworkAssemblies": {
66-
},
67-
"dependencies": {
68-
"System.ComponentModel.TypeConverter": "4.1.0"
6957
}
7058
},
7159
"net461": {
7260
"frameworkAssemblies": {
73-
},
74-
"dependencies": {
75-
"System.ComponentModel.TypeConverter": "4.1.0"
7661
}
7762
},
7863
"dnx451": {
7964
"frameworkAssemblies": {
80-
},
81-
"dependencies": {
82-
"System.ComponentModel.TypeConverter": "4.1.0"
8365
}
8466
},
8567
"netcore45": {
@@ -148,7 +130,7 @@
148130
}
149131
},
150132
"sl5": {
151-
"buildOptions": { "define": [ "SILVERLIGHT" ] },
133+
"buildOptions": { "define": [ "SL50", "SILVERLIGHT" ] },
152134
"dependencies": {
153135
"Portable.ConcurrentDictionary": "1.0.2"
154136
},

test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
23
using System.Linq.Dynamic.Core.Exceptions;
34
using System.Linq.Dynamic.Core.Tests.Helpers;
45
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
@@ -495,10 +496,13 @@ public void ExpressionTests_DateTimeString()
495496
var qry = lst.AsQueryable();
496497

497498
//Act
498-
var result1 = qry.Where("it = @0", lst[0].ToString());
499+
var testValue = lst[0].ToString(CultureInfo.InvariantCulture);
500+
var result1 = qry.Where("it = @0", testValue);
501+
var result2 = qry.Where("@0 = it", testValue);
499502

500503
//Assert
501504
Assert.Equal(lst[0], result1.Single());
505+
Assert.Equal(lst[0], result2.Single());
502506
}
503507

504508
[Fact]
@@ -511,13 +515,15 @@ public void ExpressionTests_GuidString()
511515
var qry = lst.AsQueryable();
512516

513517
//Act
514-
var result1 = qry.Where("it = @0", lst[0].ToString());
518+
var testValue = lst[0].ToString();
519+
var result1 = qry.Where("it = @0", testValue);
520+
var result2 = qry.Where("@0 = it", testValue);
515521

516522
//Assert
517523
Assert.Equal(lst[0], result1.Single());
524+
Assert.Equal(lst[0], result2.Single());
518525
}
519526

520-
521527
[Fact]
522528
public void ExpressionTests_CompareWithGuid()
523529
{
@@ -533,14 +539,18 @@ public void ExpressionTests_CompareWithGuid()
533539
//Act
534540
var result1 = qry.Where("it = \"0A191E77-E32D-4DE1-8F1C-A144C2B0424D\"");
535541
var result2 = qry.Where("\"0A191E77-E32D-4DE1-8F1C-A144C2B0424D\" = it");
536-
var result3 = qry.Where("it = @0", "0A191E77-E32D-4DE1-8F1C-A144C2B0424D");
537-
var result4 = qry.Where("it = @0", lst[2]);
542+
var result3a = qry.Where("it = @0", "0A191E77-E32D-4DE1-8F1C-A144C2B0424D");
543+
var result3b = qry.Where("@0 = it", "0A191E77-E32D-4DE1-8F1C-A144C2B0424D");
544+
var result4a = qry.Where("it = @0", lst[2]);
545+
var result4b = qry.Where("@0 = it", lst[2]);
538546

539547
//Assert
540548
Assert.Equal(lst[2], result1.Single());
541549
Assert.Equal(lst[2], result2.Single());
542-
Assert.Equal(lst[2], result3.Single());
543-
Assert.Equal(lst[2], result4.Single());
550+
Assert.Equal(lst[2], result3a.Single());
551+
Assert.Equal(lst[2], result3b.Single());
552+
Assert.Equal(lst[2], result4a.Single());
553+
Assert.Equal(lst[2], result4b.Single());
544554
}
545555

546556
[Fact]

0 commit comments

Comments
 (0)