Dynamic reference to object property when using AddMapping

1,420 views
Skip to first unread message

Sean V

unread,
Nov 17, 2010, 4:44:56 PM11/17/10
to Linq To Excel
Paul,

Is there a recommended way to reference an object property by string
when using AddMapping?

For instance:

book.AddMapping<myClass>(x =>
x.GetType().GetProperty("aClassProperty"), "Column Name");

where the string "aClassProperty" represents the name of the object
property to map?

Thanks!

Sean

Paul Yoder

unread,
Nov 18, 2010, 10:24:57 AM11/18/10
to linqt...@googlegroups.com, sve...@gmail.com
It is not possible to reference an object property by string through the AddMapping() method.
 
I recommend using the LinqToExcel.Row class in this case. Here's the documentation for it.
 

Using the LinqToExcel.Row class

Query results can be returned as LinqToExcel.Row objects which allows you to access a cell's value by using the column name in the string index. Just use the Worksheet() method without a generic argument.

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.Worksheet()
                       where c["State"] == "IN" || c["Zip"] == 46550
                       select c;

The LinqToExcel.Row class allows you to easily cast a cell's value by using its Cast<>() method

var excel = new ExcelQueryFactory("excelFileName");
var largeCompanies = from c in excel.Worksheet()
                     where c["EmployeeCount"].Cast<int>() > 500
                     select c;


hariharak...@ivymobility.com

unread,
Jul 10, 2012, 12:50:25 AM7/10/12
to linqt...@googlegroups.com
Hi,

I would like to select only the columns of excel sheet those are mapped to the properties of entity model class. In that I have a method to receive the string[] and string[] which specifies the entity model class properties and the excel column names respectively. Is it possible?

Paul Yoder

unread,
Jul 27, 2012, 9:12:49 PM7/27/12
to linqt...@googlegroups.com
That is not currently possible. You would need to create another class with only the properties you want to select.

AJ

unread,
Nov 13, 2012, 5:26:27 PM11/13/12
to linqt...@googlegroups.com, hariharak...@ivymobility.com
Hi  Did you found a work around for selecting only the columns of excel worksheet that get mapped to the properties of the entity model class ? If so could you please share... Thanks

Paul Yoder

unread,
Dec 5, 2012, 5:18:09 PM12/5/12
to linqt...@googlegroups.com, hariharak...@ivymobility.com
AJ, there's still no work around.

LinqToExcel always selects all the columns from the spreadsheet. Here's an example sql statement that is created to query the spreadsheet: "SELECT * FROM [SpreadsheetName]"

What's your use case that you don't want everything selected?

Paul 

AJ

unread,
Dec 13, 2012, 6:05:23 PM12/13/12
to linqt...@googlegroups.com, hariharak...@ivymobility.com
I have excel file
column1 column2 column3 column4
 
I want to show the user all the columns names and then they select the one column that should be mapped to the table so say they select column 3 and then show all for column 3 rows for the column that they mapped and a count of the rows should be returned.

Paul Yoder

unread,
Dec 14, 2012, 9:59:49 AM12/14/12
to linqt...@googlegroups.com, hariharak...@ivymobility.com
You can use the GetColumnNames() method to retrieve the list of columns names and then you can use the Worksheet() row class to get the data only for that field.

AJ

unread,
Dec 17, 2012, 1:12:38 PM12/17/12
to linqt...@googlegroups.com, hariharak...@ivymobility.com
Thanks for your reply Paul could you go into further detail about using the Worksheet row class to get data  and cell data.I want to return the column names and cell data to a IEnumberable<T> as this is the datasource for a grid.
For example I want to initial show all  columns names with data and then when they select the column name they want to map only show that column and its values.

Thanks in advance

I have var rows = this.Excel.Worksheet();
         var columns = this.Excel.Getcolumns(Sheetname);
         for each (var row in in rows)
            for (int a = 0; a < row cell ?????;a+++
              var  cell = row[a];
              var celldata = cell.value;
              var cellColunmName = colunmNamestst.ElementAt[a];
Reply all
Reply to author
Forward
0 new messages