Skip to content

Inherited Enumerable Support

Compare
Choose a tag to compare
@dlebee dlebee released this 31 Jul 23:12
· 1 commit to master since this release
e7a3b06

The code can now support to create expressions with class inheriting IEnumerable.

class Foo
{

}

class ListOfFoo : List<Foo>
{

}
[TestMethod]
public void TestInheritanceOfListAsGenericEnumerableType()
{
	var shouldBeTrue = QueryableHelpers.IsGenericEnumerable(typeof(ListOfFoo));
	Assert.IsTrue(shouldBeTrue);
	var type = QueryableHelpers.GetTypeOfEnumerable(typeof(ListOfFoo), true);
	Assert.IsTrue(type == typeof(Foo));
}

Allows

public class MockPerson
{
	public string Name { get; set; }
	public MockListOfPhone Phones { get; set; }
}

public class MockPhone
{
	public string Number { get; set; }
}

public class MockListOfPhone : List<MockPhone>
{
}

[TestMethod]
public void TestSelectWithInheritedList()
{
	var list = new List<MockPerson>()
	{
		new MockPerson
		{
			Name = "David Lebee",
			Phones = new MockListOfPhone
			{
				new MockPhone
				{
					Number = "0000000000"
				}
			}
		},
		new MockPerson
		{
			Name = "Yubing Liang",
			Phones = new MockListOfPhone
			{
				new MockPhone
				{
					Number = "1111111111"
				}
			}
		}
	};

	var names = list.AsQueryable()
		.Where(t => t.Equal("Phones.Number", "1111111111"))
		.Select(t =>
		{
			t.Path("Name");
			t.FirstOrDefault("Phones.Number", "Number", SelectCollectionHandling.Flatten);
		})
		.ToDynamicClassList();

	Assert.IsTrue(names.Count() == 1);
	var firstPerson = names.First();
	Assert.AreEqual("Yubing Liang", firstPerson.GetDynamicPropertyValue<string>("Name"));
	Assert.AreEqual("1111111111", firstPerson.GetDynamicPropertyValue<string>("Number"));
}