请问这样的数据源怎么用代码实现拆分?

image

演示代码链接:

https://pan.baidu.com/s/1v9LBRDxdRq-vTj-ru7tilA

提取码:c553

<%@ 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>数据源:每个种类各占一行</h4>

<asp:GridView runat="server" ID="gvSource"></asp:GridView>

<h4>目的:按数量合并,各自编号加起来数量等于数据源的各自的数量</h4>

<asp:GridView runat="server" ID="gvResult"></asp:GridView>

<pre>

说明:id为1001的Count为1(只有第一行存在);

id为1002的Count为2,1(第一行)+1(第二行);

id为1003的Count为5,1(第一行)+1(第二行)+3(第三行);

</pre>

<h4>问题:如何用代码把数据源转为目的那样的结果?</h4>

</div>

</form>

</body>

</html>

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"] = "苹果";

drNew["Count"] = 1;

dtSource.Rows.Add(drNew);

drNew = dtSource.NewRow();

drNew["Id"] = 1002;

drNew["Name"] = "香蕉";

drNew["Count"] = 2;

dtSource.Rows.Add(drNew);

drNew = dtSource.NewRow();

drNew["Id"] = 1003;

drNew["Name"] = "橙子";

drNew["Count"] = 5;

dtSource.Rows.Add(drNew);

//...实际为动态行数

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"] = "苹果,香蕉,橙子";

drResult["Count"] = 1;

dtResult.Rows.Add(drResult);

drResult = dtResult.NewRow();

drResult["Ids"] = "1002,1003";

drResult["Names"] = "香蕉,橙子";

drResult["Count"] = 1;

dtResult.Rows.Add(drResult);

drResult = dtResult.NewRow();

drResult["Ids"] = "1003";

drResult["Names"] = "橙子";

drResult["Count"] = 3;

dtResult.Rows.Add(drResult);

this.gvResult.DataSource = dtResult;

this.gvResult.DataBind();

}

}

回答:

{

class Program

{

static void Main(string[] args)

{

//读取数据库表

List<Frult> frults = new List<Frult>

{

new Frult{ Id = "1001", Name = "苹果", Count = 1 },

new Frult{ Id = "1002", Name = "香蕉", Count = 2 },

new Frult{ Id = "1003", Name = "橙子", 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; }

}

}`

以上是 请问这样的数据源怎么用代码实现拆分? 的全部内容, 来源链接: utcz.com/p/189992.html

回到顶部