Skip to content

Commit a610c73

Browse files
Provide concrete example of IContainsUnmappedCells
1 parent 05f4b8f commit a610c73

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,40 @@ var excel = new ExcelQueryFactory("excelFileName");
215215
excel.StrictMapping = StrictMappingType.Both;
216216
```
217217

218+
### Retaining Values from Unmapped Columns
219+
218220
If you are using `None` or `ClassStrict` mapping, you can retain unmapped columns by implementing the `IContainsUnmappedCells` interface. This will put all values from the unmapped columns into a dictionary on your class named `UnmappedCells`.
219221

222+
Let's say the only field you're guaranteed to have is a `Name` column, and the rest of the columns can be different per spreadsheet. You could write your `Company` class like this, implementing `IContainsUnmappedCells`:
223+
224+
```c#
225+
public class Company : IContainsUnmappedCells
226+
{
227+
public string Name { get; set; }
228+
public IDictionary<string, Cell> UnmappedCells { get; } = new Dictionary<string, Cell>();
229+
}
230+
```
231+
232+
Given the following data set:
233+
234+
Name | CEO | EmployeeCount | StartDate
235+
-------------------------------------- | ------------- | ------------- | ----------
236+
ACME | Bugs Bunny | 25 | 1918-11-11
237+
Word Made Flesh | Chris Heuertz | | 1994-08-08
238+
Anderson University | James Edwards | | 1917-09-01
239+
240+
You can query normally and all other fields will be available in the `UnmappedCells` dictionary:
241+
242+
```c#
243+
var company = from c in excel.Worksheet<Company>()
244+
where c.Name == "ACME"
245+
select c;
246+
247+
// company.UnmappedCells["CEO"] == "Bugs Bunny"
248+
// company.UnmappedCells["EmployeeCount"].Cast<int>() == 25
249+
// company.UnmappedCells["StartDate"].Cast<DateTime>() == new DateTime(1918, 11, 11)
250+
```
251+
220252
## Manually setting the database engine
221253

222254
LinqToExcel can use the Jet or Ace database engine, and it automatically determines the database engine to use by the file extension. You can manually set the database engine with the `DatabaseEngine` property

0 commit comments

Comments
 (0)