如何以编程方式将表格格式转换为字符串

时间:2016-10-14 01:27:53

标签: c# asp.net list

美好的一天,

想请求如何将表格格式转换为字符串

例如:

Material Control     August
Development         September
Planning            August
HR                  September

Septembe r:发展,人力资源
八月:物料控制,计划

            List<String> returnvalueStringMonth = new List<String>();
            List<String> returnvalueStringDept = new List<String>();
            foreach (DataRow dr in dsSeries.Tables[0].Rows)
            {
                string newmanout = dr["MonthNames"].ToString();
                returnvalueStringMonth.Add(newmanout);

                string Departs = dr["Depart"].ToString();
                returnvalueStringMonth.Add(Departs);
                //var DDLName = dr["Depart"];
                //Label dynamicLabel = new Label();
                //dynamicLabel.Text = DDLName.ToString() + ",";
                //div1.Controls.Add(dynamicLabel);
                //var sumPlus = Convert.ToDouble(newmanout) +",";

            }

            List<string> b = new List<string>();
            b.AddRange(returnvalueStringMonth.Distinct());
            for (int cs = 0; cs < b.Count; cs++)
            {
                //Panel aspPanel = new Panel();
                Label dynamicLabel = new Label();
                dynamicLabel.Text = b[cs].ToString()+":" + "<br/>";
                div1.Controls.Add(dynamicLabel);
            }

我只能在月份之前完成,然后我意识到犯了错误。 所以,请告知如何实现这个目标。

2 个答案:

答案 0 :(得分:1)

下面的代码将填充一个包含所需输出的字符串列表。您可以更改第二个循环以执行您想要的操作。

var q = from row in dsSeries.Tables[0].AsEnumerable()
        group row by row["Month"] into qGrouped
        orderby qGrouped.Key
        select String.Format("{0}: {1}", qGrouped.Key, 
                String.Join(", ", Array.ConvertAll(qGrouped.ToArray(), r => r["Department"])));

var b = q.ToList();

如果你更愿意使用LINQ,你可以这样做:

defprotocol Proto do
  def foo(proto)
end

defmodule A do
  def start_link() do
    Agent.start_link(fn -> :a end)
  end
end

defimpl Proto, for: A do
  def foo(proto) do
    Agent.get(proto, fn a -> a end)
  end
end

defmodule B do
  def start_link() do
    Agent.start_link(fn -> :b end)
  end

end

defimpl Proto, for: B do
  def foo(proto) do
    Agent.get(proto, fn b -> b end)
  end
end

{:ok, a_pid} = A.start_link()
value = Proto.foo(a_pid)

答案 1 :(得分:0)

    public class TestClass
    {
        public string S1 { get; set; }
        public string S2 { get; set; }
    }

    [TestMethod]
    public void MyTest()
    {
        //            Material Control     August
        //Development         September
        //Planning            August
        //HR                  September

        var list = new List<TestClass>
        {
            new TestClass {S1 = "Material Control", S2 = "August"},
            new TestClass {S1 = "Development", S2 = "September"},
            new TestClass {S1 = "Planning", S2 = "August"},
            new TestClass {S1 = "HR", S2 = "September"}
        };

        var listGroupByMonth = list.GroupBy(l => l.S2);
        foreach (var lstByMonth in listGroupByMonth)
        {
            var key = lstByMonth.Key;
            var finalValue = string.Join(", ", lstByMonth.ToList().Select(lbm => lbm.S1));
    }
}