我需要帮助才能使用 linq 将数据导出到 Excel。我从 linq 获得了数据,但不知道如何导出数据。我的代码如下
using (DataClassesDataContext dc = new DataClassesDataContext())
{
foreach (var mt in dc.Mapping.GetTables())
{
string sql = String.Format("Select * from {0} where ID = {1} ", mt.TableName, TenantID);
var data = dc.ExecuteQuery(mt.RowType.Type, sql);
//data is here now.
string path = "D:\\CSvFiles\\";
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
path = path + mt.TableName + ".csv";
foreach (var item in data)
{
System.IO.File.WriteAllText(path, item.ToString());
}
}
}