-
Notifications
You must be signed in to change notification settings - Fork 127
Closed
Description
Great project!
Is it possible to map properties that are an IEnumerable, like a Person that has multiple Pets?
I have an excel file like this, with five columns: one person, with two pets:
FirstName PetName1 PetAge1 PetName2 PetAge2
John Napoleon 5 Pebbles 4
and C# Person and Pet classes like this:
public class Person
{
public string FirstName { get; set; }
public List<Pet> Pets { get; set; }
public override bool Equals(object obj)
{
if (obj is not Person p) return false;
return p.FirstName == FirstName &&
p.Pets.Equals(Pets);
}
public override int GetHashCode()
{
return HashCode.Combine(FirstName, Pets);
}
}
public class Pet
{
public string PetName { get; set; }
public int PetAge { get; set; }
public override bool Equals(object obj)
{
if (obj is not Pet p) return false;
return p.PetName == PetName &&
p.PetAge.Equals(PetAge);
}
public override int GetHashCode()
{
return HashCode.Combine(PetName, PetAge);
}
}And a non working test class like this:
public class TestPerson
{
[SetUp]
public void Setup()
{
Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
}
[Test]
public void FetchPersonWithPets()
{
var excel = new ExcelMapper(@"..\..\..\Pets.xlsx") { TrackObjects = true };
excel.AddMapping<Person>("FirstName", p => p.FirstName);
excel.AddMapping<Pet>("PetName1", p => p.PetName);
excel.AddMapping<Pet>("PetName2", p => p.PetName);
excel.AddMapping<Pet>("PetAge1", p => p.PetAge);
excel.AddMapping<Pet>("PetAge2", p => p.PetAge);
var persons = excel.Fetch<Person>()
.ToList();
var expected = new Person
{
FirstName = "John",
Pets = new List<Pet>
{
new() {PetName = "Napoleon", PetAge = 5},
new() {PetName = "Pebbles", PetAge = 4}
}
};
CollectionAssert.AreEqual(new List<Person> {expected}, persons);
}
}Is something like this possible, and am I doing something wrong?
Or is it just not possible?
Metadata
Metadata
Assignees
Labels
No labels