如何映射 array/list 和 datatable/list c#
how to map array/list and datatable/list c#
我现在有一个 array/list ["a","b","c","d","e","f" ], 现在我有一个数据表 dt:
c1 c2
c 10
e 20
我想将 dt 与数组进行比较并返回结果,例如
a 0/null
b 0 /null
c 10
d 0/null
e 20
f 0/null
我不知道如何开始映射或任何程序,我尝试了一个循环但我得到了 6*2 = 12
项
任何人都可以就如何执行此操作提供一些指导吗?
我试图声明一个布尔数组,如果我能在 dt 中找到 A 中的元素,则将 true 添加到布尔数组,否则添加 false。但是我在布尔数组中得到了 12 个元素而不是 6 个元素,而且位置都是错误的
if (dt.Rows.Count > 0)
{
for (int x = 0; x < A.Length; x++)
{
for (int t = 0; t < dt.Rows.Count; t++)
{
string type = dt.Rows[t]["Sponsorship_Type"].ToString();
if (A[x] == type)
{
checks.Add(true);
}
else
{
checks.Add(false);
}
}
}
}
如果您希望输出与您在问题中显示的完全一样;
Dictionary<string, string> comparison = new Dictionary<string, string>();
foreach (string item in list)
{
DataRow[] found = dt.Select(string.Format("C1 = '{0}'", item));
if (found == null || found.Count().Equals(0))
comparison.Add(item, "0/null");
else
comparison.Add(item, found[0]["C2"].ToString());
}
list
是包含 ["a","b","c","d","e","f"]
的列表,输出在 comparison
字典中。
我现在有一个 array/list ["a","b","c","d","e","f" ], 现在我有一个数据表 dt:
c1 c2
c 10
e 20
我想将 dt 与数组进行比较并返回结果,例如
a 0/null
b 0 /null
c 10
d 0/null
e 20
f 0/null
我不知道如何开始映射或任何程序,我尝试了一个循环但我得到了 6*2 = 12
项
任何人都可以就如何执行此操作提供一些指导吗?
我试图声明一个布尔数组,如果我能在 dt 中找到 A 中的元素,则将 true 添加到布尔数组,否则添加 false。但是我在布尔数组中得到了 12 个元素而不是 6 个元素,而且位置都是错误的
if (dt.Rows.Count > 0)
{
for (int x = 0; x < A.Length; x++)
{
for (int t = 0; t < dt.Rows.Count; t++)
{
string type = dt.Rows[t]["Sponsorship_Type"].ToString();
if (A[x] == type)
{
checks.Add(true);
}
else
{
checks.Add(false);
}
}
}
}
如果您希望输出与您在问题中显示的完全一样;
Dictionary<string, string> comparison = new Dictionary<string, string>();
foreach (string item in list)
{
DataRow[] found = dt.Select(string.Format("C1 = '{0}'", item));
if (found == null || found.Count().Equals(0))
comparison.Add(item, "0/null");
else
comparison.Add(item, found[0]["C2"].ToString());
}
list
是包含 ["a","b","c","d","e","f"]
的列表,输出在 comparison
字典中。