0
所以我有一個Excel表格和一個mssql表格,並且兩個正在使用LINQ在Excel表格列0(第一個)和mssql列textfield2上進行連接。 我在加入字母數字值時看到的問題似乎無法解決。當值是數字時它確實有效。在LINQ中比較sql字段和excel字段
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excconnectionstring);
var adapter2 = new SqlDataAdapter("SELECT * FROM Table1", sqlconnectionstring);
var ds = new DataSet();
adapter.Fill(ds, "excel");
adapter2.Fill(ds, "sql");
var excel = ds.Tables["excel"].AsEnumerable();
var esqel = ds.Tables["sql"].AsEnumerable();
var query = from exc in excel
from sql in esqel
where exc[0].ToString() == sql.Field<string>("textfield2")
select new
{
debnr = sql.Field<string>("debnr"),
bedrag = double.Parse(exc[5].ToString())/100,
description = DateTime.Parse(exc[7].ToString(), new CultureInfo("nl-NL")).ToString("MMM yyyy"),
text1 = exc[0].ToString(),
projectno = sql.Field<string>("textfield1"),
central = sql.Field<string>("CentralizationAccount").Trim()
};
編輯:看來字母,當我在Excel工作表,頂部的一個字母數字值責令值做工作。但後來我有問題,它不能在數值上工作。
側面說明:使用['Join',而不是'Where'(http://stackoverflow.com/questions/5551264/why-is-linq-join當鏈接Linq-To-Objects中的表時,這要比鏈接快得多)。 –
我不能重現那個;連接在兩個表中的代碼和字符串都正常工作。我想有一些缺失。 –
您必須確保將第一列中的數值轉換爲工作表本身_中的文本。 OleDB推斷來自第一對行的Excel列的類型。與該類型不匹配的值將作爲空值傳遞。另外,觀看領先和尾隨空間。 –