我需要在我的代码中执行此操作;
将explosion_laborer和trx_date放入带
的数组key->exploded_laborer
exploded=>trx_date
我能够到达3号我只需要做第4号。我的代码如下:
private void GetLocalData()
{
const string sql = @"SELECT laborer, trx_date from tbl_jobs WHERE trx_date BETWEEN @fromDate AND @toDate";
var laborerDataTable = new DataTable();
using (var conn = new SqliteAccess().ConnectToSqlite())
{
using (var cmd = new SQLiteCommand(sql, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@fromDate", dtpFrom.Value.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("@toDate", dtpTo.Value.ToString("yyyy-MM-dd"));
laborerDataTable.Load(cmd.ExecuteReader());
}
}
var exploded = new List<string>();
foreach (DataRow row in laborerDataTable.Rows)
{
exploded.Add(row["laborer"].ToString().Split('|')[0]);
}
}
非常感谢您的帮助。
答案 0 :(得分:2)
我认为你应该创建代表键和值集合的Dictionary<TKey,TValue>
。这可能会为你做到这一点
laborerDataTable.AsEnumerable()
.Select(row => laborerDataTable.Columns.Cast<DataColumn>()
.ToDictionary(column => row[laborer] as string
column => row[trx_date] as string))
因此,完整的代码可能看起来像
private void GetLocalData()
{
const string sql = @"SELECT laborer, trx_date from tbl_jobs WHERE trx_date BETWEEN @fromDate AND @toDate";
var laborerDataTable = new DataTable();
using (var conn = new SqliteAccess().ConnectToSqlite())
{
using (var cmd = new SQLiteCommand(sql, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@fromDate", dtpFrom.Value.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("@toDate", dtpTo.Value.ToString("yyyy-MM-dd"));
laborerDataTable.Load(cmd.ExecuteReader());
}
}
var LabDict = laborerDataTable.AsEnumerable()
.Select(row => laborerDataTable.Columns.Cast<DataColumn>()
.ToDictionary(column => row[laborer] as string
column => row[trx_date] as string))
}
修改强>
这只是为了创建一个虚拟表。
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("laborer", typeof(string));
table.Columns.Add("trx_date", typeof(string));
// Here we add five DataRows.
table.Rows.Add("Indocin", "12/12/2010");
table.Rows.Add("Enebrel", "12/1/2011");
table.Rows.Add("Hydralazine", "1/12/2012");
table.Rows.Add("Combivent", "11/12/2013");
table.Rows.Add("Dilantin", "12/11/2014");
return table;
}
普通核心C#方式
DataTable laborerDataTable = GetTable();
Dictionary<string, string> exploded = new Dictionary<string, string>();
foreach(DataRow row in laborerDataTable.Rows)
{
exploded.Add(row.Field<string>(0), row.Field<string>(1));
}
另外,如何将每行打印到控制台?
foreach(var dct in exploded)
{
Console.WriteLine(dct.Key + " Date is " + dct.Value);
}
答案 1 :(得分:2)
如果您期望结果为键值对那么为什么是数组?为什么不是Dictionary<string, string>
?你可以尝试这样的事情:
Dictionary<string, string> laborerDict = laborerDataTable.AsEnumerable()
.ToDictionary(x => x.Field<string>("laborer"),
x => x.Field<string>("trx_date"));
答案 2 :(得分:0)
在java中使用内置的HashMap参考下面的代码 HashMap map = new HashMap&lt;&gt;(); Map.put(键,值); 并从地图使用中获取数据 Map.get(钥匙) 它有很多功能,也许你可以通过搜索Java API来引用它们
答案 3 :(得分:0)
当你想要更好地使用Key =&gt; Value时,你使用Dictionary而不是List
var exploded = new Dictionary<string,string>();
foreach (DataRow row in laborerDataTable.Rows)
{
exploded.Add(row["laborer"].ToString(),(row["trx_date"].ToString());
}