Skip to content

Commit 864f75f

Browse files
NickDarveyStefH
authored andcommitted
[WIP] Adds support for querying a IQueryable<dynamic> (#137)
Feature: Add support for querying a IQueryable<dynamic>
1 parent a0c358a commit 864f75f

File tree

9 files changed

+312
-214
lines changed

9 files changed

+312
-214
lines changed

src-console/ConsoleAppEF1.1/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Dynamic;
4+
using System.Linq;
35
using System.Linq.Dynamic.Core;
46
using Newtonsoft.Json;
57

src-console/ConsoleAppEF2.0/Program.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Dynamic;
34
using System.Linq;
45
using System.Linq.Dynamic.Core;
56
using System.Linq.Dynamic.Core.CustomTypeProviders;
@@ -25,9 +26,32 @@ public HashSet<Type> GetCustomTypes()
2526
}
2627
}
2728

29+
private static IEnumerable<dynamic> GetEnumerable()
30+
{
31+
var random = new Random((int)DateTime.Now.Ticks);
32+
33+
return Enumerable.Range(0, 10).Select(i => new
34+
{
35+
Id = i,
36+
Value = random.Next(),
37+
});
38+
}
39+
2840
static void Main(string[] args)
2941
{
30-
var all = new
42+
IQueryable<dynamic> qry = GetEnumerable().AsQueryable();
43+
44+
var result = qry.Select("it").OrderBy("Value");
45+
try
46+
{
47+
Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
48+
}
49+
catch (Exception e)
50+
{
51+
Console.WriteLine(e);
52+
}
53+
54+
var all = new
3155
{
3256
test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
3357
test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
+24-174
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,7 @@
1-
using System.Collections;
1+
#if !UAP10_0 && !NET35
22
using System.Collections.Generic;
3-
using System.Reflection;
4-
5-
#if UAP10_0
6-
using System.Dynamic;
7-
8-
namespace System.Linq.Dynamic.Core
9-
{
10-
/// <summary>
11-
/// Provides a base class for dynamic objects for UAP10_0.
12-
/// </summary>
13-
public class DynamicClass : DynamicObject
14-
{
15-
readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
16-
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="DynamicClass"/> class.
19-
/// </summary>
20-
/// <param name="propertylist">The propertylist.</param>
21-
public DynamicClass(params KeyValuePair<string, object>[] propertylist)
22-
{
23-
foreach (var kvp in propertylist)
24-
{
25-
_properties.Add(kvp.Key, kvp.Value);
26-
}
27-
}
28-
29-
/// <summary>
30-
/// Gets or sets the <see cref="object"/> with the specified name.
31-
/// </summary>
32-
/// <value>
33-
/// The <see cref="object"/>.
34-
/// </value>
35-
/// <param name="name">The name.</param>
36-
/// <returns>Value from the property.</returns>
37-
public object this[string name]
38-
{
39-
get
40-
{
41-
object result;
42-
if (_properties.TryGetValue(name, out result))
43-
return result;
44-
45-
return null;
46-
}
47-
set
48-
{
49-
if (_properties.ContainsKey(name))
50-
_properties[name] = value;
51-
else
52-
_properties.Add(name, value);
53-
}
54-
}
55-
56-
/// <summary>
57-
/// Returns the enumeration of all dynamic member names.
58-
/// </summary>
59-
/// <returns>
60-
/// A sequence that contains dynamic member names.
61-
/// </returns>
62-
public override IEnumerable<string> GetDynamicMemberNames()
63-
{
64-
return _properties.Keys;
65-
}
66-
67-
/// <summary>
68-
/// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
69-
/// </summary>
70-
/// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
71-
/// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result" />.</param>
72-
/// <returns>
73-
/// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
74-
/// </returns>
75-
public override bool TryGetMember(GetMemberBinder binder, out object result)
76-
{
77-
var name = binder.Name;
78-
_properties.TryGetValue(name, out result);
79-
80-
return true;
81-
}
82-
83-
/// <summary>
84-
/// Provides the implementation for operations that set member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as setting a value for a property.
85-
/// </summary>
86-
/// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member to which the value is being assigned. For example, for the statement sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
87-
/// <param name="value">The value to set to the member. For example, for sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, the <paramref name="value" /> is "Test".</param>
88-
/// <returns>
89-
/// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
90-
/// </returns>
91-
public override bool TrySetMember(SetMemberBinder binder, object value)
92-
{
93-
var name = binder.Name;
94-
if (_properties.ContainsKey(name))
95-
_properties[name] = value;
96-
else
97-
_properties.Add(name, value);
98-
99-
return true;
100-
}
101-
}
102-
}
103-
# elif NET35
104-
namespace System.Linq.Dynamic.Core
105-
{
106-
/// <summary>
107-
/// Provides a base class for dynamic objects for Net 3.5
108-
/// </summary>
109-
public abstract class DynamicClass
110-
{
111-
/// <summary>
112-
/// Gets the dynamic property by name.
113-
/// </summary>
114-
/// <typeparam name="T">The type.</typeparam>
115-
/// <param name="propertyName">Name of the property.</param>
116-
/// <returns>T</returns>
117-
public T GetDynamicPropertyValue<T>(string propertyName)
118-
{
119-
var type = GetType();
120-
var propInfo = type.GetProperty(propertyName);
121-
122-
return (T)propInfo.GetValue(this, null);
123-
}
124-
125-
/// <summary>
126-
/// Gets the dynamic property value by name.
127-
/// </summary>
128-
/// <param name="propertyName">Name of the property.</param>
129-
/// <returns>value</returns>
130-
public object GetDynamicPropertyValue(string propertyName)
131-
{
132-
return GetDynamicPropertyValue<object>(propertyName);
133-
}
134-
135-
/// <summary>
136-
/// Sets the dynamic property value by name.
137-
/// </summary>
138-
/// <typeparam name="T">The type.</typeparam>
139-
/// <param name="propertyName">Name of the property.</param>
140-
/// <param name="value">The value.</param>
141-
public void SetDynamicPropertyValue<T>(string propertyName, T value)
142-
{
143-
var type = GetType();
144-
var propInfo = type.GetProperty(propertyName);
145-
146-
propInfo.SetValue(this, value, null);
147-
}
148-
149-
/// <summary>
150-
/// Sets the dynamic property value by name.
151-
/// </summary>
152-
/// <param name="propertyName">Name of the property.</param>
153-
/// <param name="value">The value.</param>
154-
public void SetDynamicPropertyValue(string propertyName, object value)
155-
{
156-
SetDynamicPropertyValue<object>(propertyName, value);
157-
}
158-
}
159-
}
160-
#else
1613
using System.Dynamic;
4+
using System.Reflection;
1625

1636
namespace System.Linq.Dynamic.Core
1647
{
@@ -177,7 +20,7 @@ public abstract class DynamicClass : DynamicObject
17720
{
17821
private Dictionary<string, object> _propertiesDictionary;
17922

180-
private Dictionary<string, object> _properties
23+
private Dictionary<string, object> Properties
18124
{
18225
get
18326
{
@@ -253,28 +96,31 @@ public void SetDynamicPropertyValue(string propertyName, object value)
25396
/// <summary>
25497
/// Gets or sets the <see cref="object"/> with the specified name.
25598
/// </summary>
256-
/// <value>
257-
/// The <see cref="object"/>.
258-
/// </value>
99+
/// <value>The <see cref="object"/>.</value>
259100
/// <param name="name">The name.</param>
260101
/// <returns>Value from the property.</returns>
261102
public object this[string name]
262103
{
263104
get
264105
{
265-
object result;
266-
if (_properties.TryGetValue(name, out result))
106+
if (Properties.TryGetValue(name, out object result))
107+
{
267108
return result;
109+
}
268110

269111
return null;
270112
}
271113

272114
set
273115
{
274-
if (_properties.ContainsKey(name))
275-
_properties[name] = value;
116+
if (Properties.ContainsKey(name))
117+
{
118+
Properties[name] = value;
119+
}
276120
else
277-
_properties.Add(name, value);
121+
{
122+
Properties.Add(name, value);
123+
}
278124
}
279125
}
280126

@@ -286,7 +132,7 @@ public object this[string name]
286132
/// </returns>
287133
public override IEnumerable<string> GetDynamicMemberNames()
288134
{
289-
return _properties.Keys;
135+
return Properties.Keys;
290136
}
291137

292138
/// <summary>
@@ -300,7 +146,7 @@ public override IEnumerable<string> GetDynamicMemberNames()
300146
public override bool TryGetMember(GetMemberBinder binder, out object result)
301147
{
302148
string name = binder.Name;
303-
_properties.TryGetValue(name, out result);
149+
Properties.TryGetValue(name, out result);
304150

305151
return true;
306152
}
@@ -316,13 +162,17 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
316162
public override bool TrySetMember(SetMemberBinder binder, object value)
317163
{
318164
string name = binder.Name;
319-
if (_properties.ContainsKey(name))
320-
_properties[name] = value;
165+
if (Properties.ContainsKey(name))
166+
{
167+
Properties[name] = value;
168+
}
321169
else
322-
_properties.Add(name, value);
170+
{
171+
Properties.Add(name, value);
172+
}
323173

324174
return true;
325175
}
326176
}
327177
}
328-
#endif
178+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#if NET35
2+
namespace System.Linq.Dynamic.Core
3+
{
4+
/// <summary>
5+
/// Provides a base class for dynamic objects for Net 3.5
6+
/// </summary>
7+
public abstract class DynamicClass
8+
{
9+
/// <summary>
10+
/// Gets the dynamic property by name.
11+
/// </summary>
12+
/// <typeparam name="T">The type.</typeparam>
13+
/// <param name="propertyName">Name of the property.</param>
14+
/// <returns>T</returns>
15+
public T GetDynamicPropertyValue<T>(string propertyName)
16+
{
17+
var type = GetType();
18+
var propInfo = type.GetProperty(propertyName);
19+
20+
return (T)propInfo.GetValue(this, null);
21+
}
22+
23+
/// <summary>
24+
/// Gets the dynamic property value by name.
25+
/// </summary>
26+
/// <param name="propertyName">Name of the property.</param>
27+
/// <returns>value</returns>
28+
public object GetDynamicPropertyValue(string propertyName)
29+
{
30+
return GetDynamicPropertyValue<object>(propertyName);
31+
}
32+
33+
/// <summary>
34+
/// Sets the dynamic property value by name.
35+
/// </summary>
36+
/// <typeparam name="T">The type.</typeparam>
37+
/// <param name="propertyName">Name of the property.</param>
38+
/// <param name="value">The value.</param>
39+
public void SetDynamicPropertyValue<T>(string propertyName, T value)
40+
{
41+
var type = GetType();
42+
var propInfo = type.GetProperty(propertyName);
43+
44+
propInfo.SetValue(this, value, null);
45+
}
46+
47+
/// <summary>
48+
/// Sets the dynamic property value by name.
49+
/// </summary>
50+
/// <param name="propertyName">Name of the property.</param>
51+
/// <param name="value">The value.</param>
52+
public void SetDynamicPropertyValue(string propertyName, object value)
53+
{
54+
SetDynamicPropertyValue<object>(propertyName, value);
55+
}
56+
}
57+
}
58+
#endif

0 commit comments

Comments
 (0)