如何解决如何拆分此数据表?
请看图片说明: https://i.stack.imgur.com/65enL.jpg
数据源:每个类别一行
目的:按数量合并,它们各自的数量之和等于数据源的各自数量
注意:ID为1001的计数为1(仅存在第一行);
ID为1002的计数为2,1(第一行)+1(第二行);
ID为1003的计数为5,1(第一行)+1(第二行)+3(第三行);
问题:如何将数据源编码到目标位置?
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Develop_BBS_DataSplitDemo : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
var dtSource = new DataTable();
dtSource.Columns.Add("Id",typeof(int));
dtSource.Columns.Add("Name",typeof(string));
dtSource.Columns.Add("Count",typeof(int));
var drNew = dtSource.NewRow();
drNew["Id"] = 1001;
drNew["Name"] = "Apple";
drNew["Count"] = 1;
dtSource.Rows.Add(drNew);
drNew = dtSource.NewRow();
drNew["Id"] = 1002;
drNew["Name"] = "Banana";
drNew["Count"] = 2;
dtSource.Rows.Add(drNew);
drNew = dtSource.NewRow();
drNew["Id"] = 1003;
drNew["Name"] = "Orange";
drNew["Count"] = 5;
dtSource.Rows.Add(drNew);
//...The actual number of dynamic rows
this.gvSource.DataSource = dtSource;
this.gvSource.DataBind();
var dtResult = new DataTable();
dtResult.Columns.Add("Ids",typeof(string));
dtResult.Columns.Add("Names",typeof(string));
dtResult.Columns.Add("Count",typeof(int));
var drResult = dtResult.NewRow();
drResult["Ids"] = "1001,1002,1003";
drResult["Names"] = "Apple,Banana,Orange";
drResult["Count"] = 1;
dtResult.Rows.Add(drResult);
drResult = dtResult.NewRow();
drResult["Ids"] = "1002,1003";
drResult["Names"] = "Banana,Orange";
drResult["Count"] = 1;
dtResult.Rows.Add(drResult);
drResult = dtResult.NewRow();
drResult["Ids"] = "1003";
drResult["Names"] = "Orange";
drResult["Count"] = 3;
dtResult.Rows.Add(drResult);
this.gvResult.DataSource = dtResult;
this.gvResult.DataBind();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataSplitDemo.aspx.cs" Inherits="Develop_BBS_DataSplitDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Data source: one row for each category</h4>
<asp:GridView runat="server" ID="gvSource"></asp:GridView>
<h4>Purpose: merge by quantity,and the sum of their respective numbers is equal to the respective quantity of the data source</h4>
<asp:GridView runat="server" ID="gvResult"></asp:GridView>
<pre>
Note: count with ID 1001 is 1 (only the first row exists);
The count with ID 1002 is 2,1 (the first row) + 1 (the second row);
The count with id 1003 is 5,1 (the first row) + 1 (the second row) + 3 (the third row);
</pre>
<h4>Question: how to code a data source into a destination?</h4>
</div>
</form>
</body>
</html>
解决方法
class Program
{
static void Main(string[] args)
{
List<Frult> frults = new List<Frult>
{
new Frult{ Id = "1001",Name = "Apple",Count = 1 },new Frult{ Id = "1002",Name = "Banana",Count = 2 },new Frult{ Id = "1003",Name = "Orange",Count = 5 },};
List<Frult> frultsRs = new List<Frult>();
while (true)
{
frults.RemoveAll(f => f.Count <= 0);
if (frults.Count == 0) break;
var doneIds = frults.Select(c => c.Id);
string id = string.Join(',',doneIds);
var frultGroup = frultsRs.FirstOrDefault(f => f.Id == id);
if (frultGroup == null) frultsRs.Add(new Frult
{
Id = id,Name = string.Join(',frults.Select(c => c.Name)),Count = 1
});
else
{
frultGroup.Count += 1;
}
frults.ForEach(f => f.Count -= 1);
}
foreach (var d in frultsRs)
{
Console.WriteLine(string.Format("{0},{1},{2}",d.Id,d.Name,d.Count));
}
Console.ReadKey();
}
}
public class Frult
{
public string Id { get; set; }
public string Name { get; set; }
public int Count { get; set; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。