a 3
b 5
c 9
d 1
b 7上边中有相同的b,合并后效果
a 3
b 12
c 9
d 1        /*List<DataClass.jx_so> listk = new List<DataClass.jx_so>();        List<string> listL = new List<string>();
        List<string> listL2 = new List<string>();
        for (int k = 0; k < listk.Count; k++)
        {
            //if (listL.IndexOf(listk[k]) == -1)
            //    listL.Add();
        }
*/求高人帮忙解决一下!

解决方案 »

  1.   

    本帖最后由 caozhy 于 2012-04-19 16:01:55 编辑
      

  2.   

    本帖最后由 caozhy 于 2012-04-19 16:00:29 编辑
      

  3.   

    本帖最后由 caozhy 于 2012-04-19 15:59:06 编辑
      

  4.   

    if (listk != null)
                {
                    var qwlist = from wp in listk
                                 group wp by wp.Part_no into g
                                 let sum = g.Sum(s => s.Qty)
                                 select new
                                 {
                                     Part_no = g.Key,
                                     Model=g.Key,
                                     Color=g.Key,
                                     ysum = sum
                                 };
                    foreach (var pq in qwlist)
                    {
                        Response.Write(pq.Model+"-"+pq.Color + "  " + pq.ysum.ToString());
                        //Console.WriteLine(pq.x + "  " + pq.ysum.ToString());
                    }
    我response.write出的,model,color这些参数都一样,都是part_no,怎么写可以多加参数进去
      

  5.   

    不就是你写的那样,可能我理解错了                         var qwlist = from wp in listL
                             group wp by wp.x into g
                             let sum = g.Sum(s => s.y)
                             select new
                             {
                                 x = g.Key,
                                 x1 = g.Key,
                                 x2 = g.Key,
                                 ysum = sum
                             };
                foreach (var pq in qwlist)
                {
                    Console.WriteLine(pq.x + "  " + pq.x1 + " " + pq.x2 + " " + pq.ysum.ToString());
                }
      

  6.   

    我的意思是:
    public jx_so(string x, int y,string A,string B,string C) : this()
                {
                    X = x;
                    Y = y;
                    A=a;
                    B=B;
                    C=c;

                }里边的参数多了好多,但还是以x为分组,以这个为准合并
      

  7.   

    可以的啊,多写几个let就行了啊
      

  8.   

    直接用dictionary 不是更好msdn下吧
      

  9.   

    用 dictionary吧
      插入时候不会重复的
    /// <summary>
    /// 插入数据
    /// </summary>
    /// <param name="name"></param>
    /// <param name="number"></param>
    /// <res></res>
    private void AddData(string name, int number)
    {
    if (mydata.ContainsKey(name)) {
    mydata[name] += number;
    } else {
    mydata[name] = number;
    }
    }
    /// <summary>
    /// 申明字典
    /// </summary>
    /// <res></res>
    Dictionary<string, int> mydata = new Dictionary<string, int>();
    /// <summary>
    /// 遍历字典
    /// </summary>
    /// <res></res>
    private void ForData()
    {
    foreach (KeyValuePair<string, int> subdata in mydata) {
    string a = subdata.Key;
    //键值 如你的abcde
    int b = subdata.Value;
    //数值总数
    }
    }
      

  10.   

    楼上老大的方法,我试了一下,但我一条数据有多个参数我数据格式如下partno    model color   qty0001      本本     黑色     30002      笔     红色     60001      本本     黑色     20003      小本     白色     9
    我用您的方法,结果把各各参数给加起来了。
    变成了 00010001 本本本本。
      

  11.   

    KeyValuePair<string, int>
    里面的int 你可以改成1个自己写的class类嘛 那个只是1个简单的作品
      

  12.   

    /// <summary>
        /// 声明一个产品类,用来存储多个单子合并后的产品记录
        /// </summary>
        public class Pro
        {
            private string model, color, change_type, tax, customer_name, part_no;
            private double unitprice;        public Pro(string Model, string Color, string Part_no, string Change_type, string Tax, string Customer_name, double Unitprice)
            {
                this.model = Model;
                this.color = Color;
                this.part_no = Part_no;
                this.change_type = Change_type;
                this.tax = Tax;
                this.customer_name = Customer_name;
                this.unitprice = Unitprice;
            }        public string Model
            {
                get { return model; }
                set { model = value; }
            }
            public string Color
            {
                get { return color; }
                set { color = value; }
            }
            public string Change_type
            {
                get { return change_type; }
                set { change_type = value; }
            }
            public string Tax
            {
                get { return tax; }
                set { tax = value; }
            }
            public string Customer_name
            {
                get { return customer_name; }
                set { customer_name = value; }
            }
            public string Part_no
            {
                get { return part_no; }
                set { part_no = value; }
            }
        }
    还是弄的不对。。
      

  13.   

    我的new  DataClass.jx_so 和我15楼发的Pro差不多,listk = list.GroupBy(l => l.x).Select(g => new DataClass.jx_so{ x = g.Key, y = g.Sum(a => a.y) }).ToList();
    所以红色部分会有问题