[英]Group a Datatable and copy to another table C#
這里已經有一個問題“ https://stackoverflow.com/questions/27121248/report-generation-in-asp.net ”。 我有同樣的情況,我在一個特定的點上存貨。
請幫忙。
我的 DataTable 有三列
Name Plot Area
a 12 .90
a 13 .30
b 12 .45
b 13 .46
c 13 .98
我希望我的結果是
Name Plot Area
a 12 .90
13 .30
b 12 .45
13 .46
c 13 .98
這就是我嘗試過的。 我已將其分組到 linq 並需要復制到另一個表。 我怎樣才能做到這一點:
static class Program
{
static void Main(string[] args)
{
try
{
DataTable dtMain = new DataTable();
dtMain.Columns.Add("Name", typeof(string));
dtMain.Columns.Add("Plot", typeof(int));
dtMain.Columns.Add("Area", typeof(decimal));
DataRow dr1 = dtMain.NewRow();
dr1["Name"] = "a";
dr1["Plot"] = 12;
dr1["Area"] = .90;
dtMain.Rows.Add(dr1);
DataRow dr2 = dtMain.NewRow();
dr2["Name"] = "a";
dr2["Plot"] = 13;
dr2["Area"] = .30;
dtMain.Rows.Add(dr2);
DataRow dr3 = dtMain.NewRow();
dr3["Name"] = "b";
dr3["Plot"] = 12;
dr3["Area"] = .45;
dtMain.Rows.Add(dr3);
DataRow dr4 = dtMain.NewRow();
dr4["Name"] = "b";
dr4["Plot"] = 13;
dr4["Area"] = .46;
dtMain.Rows.Add(dr4);
DataRow dr5 = dtMain.NewRow();
dr5["Name"] = "c";
dr5["Plot"] = 13;
dr5["Area"] = .98;
dtMain.Rows.Add(dr5);
DataTable dtFinal = dtMain.Clone();
var groupedData = (from b in dtMain.AsEnumerable()
group b by b.Field<string>("Name") into g
select new
{
Name = g.Key,
Plot = g.Select(x => x.Field<int>("Plot")),
Area = g.Select(x => x.Field<Decimal>("Area")),
}).ToList();
//Next what should i do here.CopyToDataTable() no longer getting the correct data.
dtFinal = groupedData.ToList().CopyToDataTable();
Console.ReadLine();
}
catch (Exception objEx)
{
Console.WriteLine(objEx);
}
}
}
如果要使“ Name
僅顯示一次,則需要更改查詢:
dtMain.AsEnumerable()
.GroupBy(b => b.Field<string>("Name"))
.SelectMany(g => g.Select((x,idx) => new
{
Name = idx == 0 ? g.Key : "",
Plot = x.Field<int>("Plot"),
Area = x.Field<Decimal>("Area"),
}).ToList();
int index = 0;
bool saveIndex = false;
DataTable table = sourceTable.Copy();
for(int i=0; i< table.Rows.Count; i++)
{
if(i==0)
continue;
if(!saveIndex)
index = i-1;
if(table.Rows[i]["Name"] == table.Rows[index]["Name"])
{
table.Rows[i]["Name"] = "";
saveIndex = true;
}
else
{
saveIndex = false;
}
}
我不知道您所從事領域的限制,但這可能會有所幫助。
我在先前的回答中將此內容寫給您,希望對您有所幫助!
不要害怕使用循環;)
var nameGroups = dtMain.AsEnumerable().GroupBy(row => row.Field<string>("Name"));
foreach (var nameGroup in nameGroups)
{
bool isFirstRow = true;
foreach (DataRow row in nameGroup)
{
DataRow newRow = dtFinal.Rows.Add();
newRow.SetField("Name", isFirstRow ? nameGroup.Key : "");
newRow.SetField("Plot", row.Field<int>("Plot"));
newRow.SetField("Area", row.Field<decimal>("Area"));
isFirstRow = false;
}
}
LINQ不應引起副作用。 因此,使用它來選擇創建表所需的內容。
結果:
a 12 0.9
13 0.3
b 12 0.45
13 0.46
c 13 0.98
如果您堅持要避免循環,這就是您應該使用它們的原因。 看一下這個難看,低效和奇怪的查詢:
DataTable dtFinal = dtMain.Clone();
dtFinal = dtMain.AsEnumerable()
.GroupBy(row => row.Field<string>("Name"))
.SelectMany(grp => grp.Select((row, index)=> index == 0
? dtFinal.LoadDataRow(new object[]{ grp.Key, row.Field<int>("Plot"), row.Field<decimal>("Area")}, true)
: dtFinal.LoadDataRow(new object[]{ "", row.Field<int>("Plot"), row.Field<decimal>("Area")}, true)))
.CopyToDataTable();
var query = from row in table.AsEnumerable()
group row by row.Field<string>("RoomName") into grp
select new
{
RoomName = grp.Key,
WeekDuty = grp.First().Field<string>("WeekDuty")
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.