我有一张Excel表格,其中包含列和列映射列表。
List<Mapping> _mapping
Mapping是一个包含两个字符串的类
public class Mapping
{
public string ExcelColumn { get; set; } //the excel column name
public string MapTo { get; set; } //the column in sql database that it maps to
}
基本上,我正在尝试将两个数据集结合起来,类似于SQL WHERE [field1] IN [field2]
子句。但我有两个问题:
代码:
var allActivity = GetActivity();
using (DataTable excelData = GetExcelDataAsTable()) //get datatable representation of EXCEL SHEET
{
var excelDataRows = excelData.AsEnumerable();
//do this for each field mapped
foreach(Mapping fieldMapping in _mapping)
{
var result = from activity in allActivity //where clause contains errors
where activity[fieldMapping.MapTo].Contains(excelDataRows.Field<string>(fieldMapping.ExcelColumn))
select activity;
}
}
这是GetActivity()
,它返回一个IEnumerable
private IEnumerable<TradeActivity> GetActivity()
答案 0 :(得分:0)
想出来......
首先,我们需要能够通过字符串名称引用类属性。将其添加到班级
public object this[string propertyName]
{
get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
}
需要隔离其中一个数据集:
var excelColumn = excelData.AsEnumerable().Select(r => r[fieldMapping.ExcelColumn]);
然后执行where子句:
var result = from activity in allActivity
where excelColumn.Contains(activity[fieldMapping.MapTo])
select activity;