Skip to content

Commit d462fe8

Browse files
committed
Support field access in AccessMultipleMembers; add test
Enhanced AccessMultipleMembers to handle both properties and fields when traversing member names, improving flexibility and robustness. Added a unit test to verify correct OData filter generation when using constant object properties in expressions.
1 parent 3a49368 commit d462fe8

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/Linq2OData.Core/Expressions/FilterExpressionVisitor.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,18 +623,24 @@ private static string GetODataMemberName(MemberInfo member)
623623
{
624624
foreach (var accessName in memberAccessNames)
625625
{
626-
var property = value.GetType().GetProperty(accessName);
627-
if (property == null)
626+
var type = value.GetType();
627+
var property = type.GetProperty(accessName);
628+
if (property != null)
628629
{
629-
throw new NotSupportedException($"Property '{accessName}' not found on type '{value.GetType().Name}'");
630+
var nextValue = property.GetValue(value);
631+
if (nextValue == null) return null;
632+
value = nextValue;
630633
}
631-
632-
var nextValue = property.GetValue(value);
633-
if (nextValue == null)
634+
else
634635
{
635-
return null;
636+
var field = type.GetField(accessName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
637+
if (field == null)
638+
throw new NotSupportedException($"Property or field '{accessName}' not found on type '{type.Name}'");
639+
640+
var nextValue = field.GetValue(value);
641+
if (nextValue == null) return null;
642+
value = nextValue;
636643
}
637-
value = nextValue;
638644
}
639645
return value;
640646
}

test/Linq2OData.Tests/FilterExpressionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ public void ODataFilterVisitor_GreaterThanOrEqual_GeneratesCorrectFilter()
260260
Assert.Equal("(Stock ge 10)", result);
261261
}
262262

263+
[Fact]
264+
public void ODataFilterVisitor_ConstantAsObject_GeneratesCorrectFilter()
265+
{
266+
267+
var product = new TestProduct { ID = 10, Name = "Test", Price = 9.99m, Stock = 100, IsAvailable = true, CreatedDate = DateTime.Now, LastModified = DateTimeOffset.Now, OpenTime = TimeOnly.FromTimeSpan(TimeSpan.FromHours(9)), OpenDate = DateOnly.FromDateTime(DateTime.Today) };
268+
269+
// Arrange
270+
var visitor = new ODataFilterVisitor();
271+
Expression<Func<TestProduct, bool>> expression = p => p.Stock >= product.ID;
272+
273+
// Act
274+
var result = visitor.ToFilter(expression, ODataVersion.V4);
275+
276+
// Assert
277+
Assert.Equal("(Stock ge 10)", result);
278+
}
279+
280+
263281
#endregion
264282

265283
#region Boolean Tests

0 commit comments

Comments
 (0)