C# 将列值设为标题列
我的表1有一些标题和值:C# 将列值设为标题列,c#,linq,header,row,var,C#,Linq,Header,Row,Var,我的表1有一些标题和值: KundeID KundeName Produkt Comment 1 Michael Jogurt "nichts" 2 Raj "Ich bin cool" 3 Gary Fanta "yahoo" 4 Miky Sprite 我想切换到表2,将Produkt中的值作为标题列: KundeID KundeName C
KundeID KundeName Produkt Comment
1 Michael Jogurt "nichts"
2 Raj "Ich bin cool"
3 Gary Fanta "yahoo"
4 Miky Sprite
我想切换到表2,将Produkt中的值作为标题列:
KundeID KundeName Comment Jogurt Fanta Sprite
1 Michael "nichts" x
2 Raj "Ich bin cool"
3 Gary "yahoo" x
4 Miky x
我的表1代码:
DataTable table = new DataTable("Kunde");
table.Columns.Add("KundeID", typeof(Int32));
table.Columns.Add("KundeName", typeof(String));
table.Columns.Add("Produkt", typeof(String));
DataTable comment = new DataTable("Comment");
comment.Columns.Add("KundeName", typeof(String));
comment.Columns.Add("Comment", typeof(String));
DataSet ds = new DataSet("DataSet");
ds.Tables.Add(table);
ds.Tables.Add(comment);
object[] o1 = { 1, "Michael", "Jogurt" };
object[] o2 = { 2, "Raj" };
object[] o3 = { 3, "Gary", "Fanta" };
object[] o4 = { 4, "Miky", "Sprite" };
object[] c1 = { "Raj", "Ich bin cool" };
object[] c2 = { "Gary", "yahoo" };
object[] c3 = { "Michael", "nichts" };
table.Rows.Add(o1);
table.Rows.Add(o2);
table.Rows.Add(o3);
table.Rows.Add(o4);
comment.Rows.Add(c1);
comment.Rows.Add(c2);
comment.Rows.Add(c3);
var results = from table1 in table.AsEnumerable()
join table2 in comment.AsEnumerable()
on table1.Field<string>("KundeName") equals table2.Field<string>("KundeName") into prodGroup
from table4 in prodGroup.DefaultIfEmpty()
select new
{
KundeID = table1.Field<Int32?>("KundeID"),
KundeName = table1.Field<String>("KundeName"),
Produkt = table1.Field<String>("Produkt"),
Comment = table4 != null ? table4.Field<String>("Comment") : null,
};
dataGridView1.DataSource = results.ToList();
DataTable=新的DataTable(“Kunde”);
表.列.添加(“KundeID”,typeof(Int32));
表.Columns.Add(“KundeName”,typeof(String));
表.Columns.Add(“Produkt”,typeof(String));
DataTable注释=新的DataTable(“注释”);
Add(“KundeName”,typeof(String));
comment.Columns.Add(“comment”,typeof(String));
数据集ds=新数据集(“数据集”);
ds.Tables.Add(表);
添加(注释);
对象[]o1={1,“Michael”,“Jogurt”};
对象[]o2={2,“Raj”};
对象[]o3={3,“Gary”,“Fanta”};
对象[]o4={4,“米基”,“精灵”};
对象[]c1={“Raj”,“Ich bin cool”};
对象[]c2={“Gary”,“yahoo”};
object[]c3={“Michael”,“nichts”};
表.行.添加(o1);
表.行.添加(o2);
表.行.添加(o3);
表.行.添加(o4);
comment.Rows.Add(c1);
comment.Rows.Add(c2);
comment.Rows.Add(c3);
var results=来自表1中的表.AsEnumerable()
在comment.AsEnumerable()中连接表2
在表1中,字段(“KundeName”)等于表2。字段(“KundeName”)进入PRODGOUP
来自prodGroup.DefaultIfEmpty()中的表4
选择新的
{
KundeID=表1.字段(“KundeID”),
KundeName=表1.字段(“KundeName”),
Produkt=表1.字段(“Produkt”),
Comment=table4!=null?table4.字段(“Comment”):null,
};
dataGridView1.DataSource=results.ToList();
如何从“Produkt”中获取值并使其成为标题?谢谢你们的帮助试试下面的内容……这可能会对你们有所帮助
DataTable table = new DataTable("Kunde");
table.Columns.Add("KundeID", typeof(Int32));
table.Columns.Add("KundeName", typeof(String));
table.Columns.Add("Produkt", typeof(String));
table.Columns.Add("Comment", typeof(String));
object[] o1 = { 1, "Michael", "Jogurt", "nichts" };
object[] o2 = { 2, "Raj","","Ich bin cool" };
object[] o3 = { 3, "Gary", "Fanta","yahoo" };
object[] o4 = { 4, "Miky", "Sprite","" };
table.Rows.Add(o1);
table.Rows.Add(o2);
table.Rows.Add(o3);
table.Rows.Add(o4);
Dictionary<int,string> dictObj=new Dictionary<int, string>();
for (int i = 0; i < table.Rows.Count; i++)
{
dictObj.Add(Convert.ToInt32(table.Rows[i][0].ToString()), table.Rows[i][2].ToString());
}
foreach (var obj in dictObj)
{
if (string.IsNullOrEmpty(obj.Value))
{
table.Columns.Add(obj.Value, typeof(String));
DataRow row = table.Rows[obj.Key];
row[obj.Value] = "X";
}
}
DataTable=新的DataTable(“Kunde”);
表.列.添加(“KundeID”,typeof(Int32));
表.Columns.Add(“KundeName”,typeof(String));
表.Columns.Add(“Produkt”,typeof(String));
表.列.添加(“注释”,类型(字符串));
对象[]o1={1,“Michael”,“Jogurt”,“nichts”};
对象[]o2={2,“Raj”,“Ich bin cool”};
对象[]o3={3,“Gary”,“Fanta”,“yahoo”};
对象[]o4={4,“Miky”,“Sprite”,““”};
表.行.添加(o1);
表.行.添加(o2);
表.行.添加(o3);
表.行.添加(o4);
Dictionary dictObj=新字典();
for(int i=0;i
无论出现了多少种不同的产品,这都应该能起到作用。它非常简短
// build the new data table
var result = new DataTable();
result.Columns.Add("KundeID", typeof(Int32));
result.Columns.Add("KundeName", typeof(String));
result.Columns.Add("Comment", typeof(String));
result.Columns.AddRange(
(from c in
(from r in table.AsEnumerable()
where !r.IsNull("Produkt") && !string.IsNullOrEmpty(r.Field<string>("Produkt"))
select r.Field<string>("Produkt")).Distinct() // added DISTINCT
select new DataColumn(c, typeof(bool))).ToArray()
);
foreach (var r in results)
{
var productIndex = result.Columns.IndexOf(r.Produkt);
var vals = new List<object>() { r.KundeID, r.KundeName, r.Comment };
for (int i = 3; i < result.Columns.Count; i++)
{
if (i == productIndex)
{
vals.Add(true);
}
else
{
vals.Add(false);
}
}
result.LoadDataRow(vals.ToArray(), true);
}
//构建新的数据表
var result=新数据表();
添加(“KundeID”,typeof(Int32));
Add(“KundeName”,typeof(String));
Add(“Comment”,typeof(String));
result.Columns.AddRange(
(摘自中的c)
(来自表中的r.AsEnumerable()
其中!r.IsNull(“Produkt”)和&!string.IsNullOrEmpty(r.Field(“Produkt”))
选择r.Field(“Produkt”).Distinct()//添加了Distinct
选择新数据列(c,typeof(bool))).ToArray()
);
foreach(结果中的var r)
{
var productIndex=result.Columns.IndexOf(r.Produkt);
var vals=new List(){r.KundeID,r.KundeName,r.Comment};
for(int i=3;i
非常感谢,您的代码非常有效,当我在一个对象中有更多的“Produkts”时,例如1 Michael Jogurt、Fanta“nichts”@UniLe,我很高兴我能提供帮助;表.行.添加(o5);它显示了Douplicate的错误,我应该怎么做?再次感谢,我还有一个问题,当我有object[]o6={6,“HH”,“Sprit,Fanta”};这意味着我有Sprit和Fanta,所以应该得到2x,我如何更改代码才能做到这一点?@UniLe,您应该能够运行Linq查询来对结果表进行分组,并显示您想要的结果。dataGridView2.DataSource=newtable;当我想要查看结果时,它会忽略注释,只显示没有行的表Comment@UniLe因为我创建了您的表的副本。它既不包含注释也不包含注释。如何处理表和注释?我的意思是从表和注释中复制,然后用newtable连接它?
var products = table.AsEnumerable()
.GroupBy(c => c["Produkt"])
.Where(g => !(g.Key is DBNull))
.Select(g => (string)g.Key)
.ToList();
var newtable = table.Copy();
products.ForEach(p=>newtable.Columns.Add(p,typeof(bool)));
foreach (var row in newtable.AsEnumerable())
{
if (!(row["Produkt"] is DBNull)) row[(string)row["Produkt"]] = true;
}
newtable.Columns.Remove("Produkt");