1
public static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
{
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
return dataTable;
}
}
static void Main(string[] args)
{
var dir = AppDomain.CurrentDomain.BaseDirectory + @"\classes.csv";
var test = GetDataTableFromCsv(dir, true);
var totalclasses = test.Rows.Count;
Console.Write("Number of Classes: " + totalclasses);
Console.WriteLine();
test.Columns[0].ColumnName = "Studios";
Console.Write("Col1: " + test.Columns[0].ColumnName);
Console.WriteLine();
Console.Write("Col2: " + test.Columns[1].ColumnName);
}
這是我的代碼。下一步是瀏覽名爲'studios'的列,並計算出有多少值不同。我會如何去做這件事?在DataTable上使用select distinct選項
我繼續努力,但得到:
無法轉換lambda表達式輸入字符串,因爲它不是一個 委託類型LINQ。
我在我的代碼中也有using System.Linq;
聲明。
[在這裏輸入的形象描述] [1] [1]:http://i.stack.imgur.com/8ZuOH.jpg 我得到這個例外。 – hatfieldac
@ user1795631,這意味着你的列的類型爲'int'。我已經更新了答案,希望能夠奏效。 – Habib
我似乎仍然得到相同的錯誤。 – hatfieldac