初学Linq,对linq操作List不太熟悉,现有以下三点困惑,望各位多多指教:
假设有一个用户类,包括了存款帳號,存款名,存款金額和一个DataTable,简单的代码如下:
static void Main(string[] args)
        {
            List<user> list = new List<user>();
            list.Add(new user() { struserNo = "A1", struserName = "張三", deposit = 10000,dtUser = null});
            list.Add(new user() { struserNo = "A1", struserName = "張三", deposit = 20000,dtUser = null});            list.Add(new user() { struserNo = "A2", struserName = "李四", deposit = 30000,dtUser = null});
            list.Add(new user() { struserNo = "A2", struserName = "李四", deposit = 40000,dtUser = null});            list.Add(new user() { struserNo = "A3", struserName = "王五", deposit = 44000,dtUser = null});
        }
        class user
        {
            public string struserNo { get; set; }//存款帳號
            public string struserName { get; set; }//存款名
            public int deposit{ get; set; }//存款金額
            public DataTable dtUser { get; set; }//一個user的DataTable
        }
代码里list.add()了几个用户,如果现在我想做以下三个操作该怎么去写代码呢?
1:取出所有存款帳號(struserNo)为“A1”的存款金額(deposit)之和
2:取出存款金額(deposit)为“40000”并且存款帳號(struserNo)为“A2”的存款名(struserName)
3: 如何在每一次执行list。add()事件之后都为user类里的dtUser(DataTable类型)加上一行struserNo = "存款帳號",    struserName = "存款名", deposit = “存款金額”?(即是dtUser = null要如何作修改才能达到第三点要求?)      

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;namespace AAA
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<user> list = new List<user>();
                list.Add(new user() { struserNo = "A1", struserName = "張三", deposit = 10000, dtUser = null });
                list.Add(new user() { struserNo = "A1", struserName = "張三", deposit = 20000, dtUser = null });
                list.Add(new user() { struserNo = "A1", struserName = "張三1", deposit = 20000, dtUser = null });
                list.Add(new user() { struserNo = "A1", struserName = "張三2", deposit = 20000, dtUser = null });
                list.Add(new user() { struserNo = "A1", struserName = "張三3", deposit = 20000, dtUser = null });            list.Add(new user() { struserNo = "A2", struserName = "李四", deposit = 30000, dtUser = null });
                list.Add(new user() { struserNo = "A2", struserName = "李四", deposit = 40000, dtUser = null });
                list.Add(new user() { struserNo = "A2", struserName = "李四1", deposit = 40000, dtUser = null });
                list.Add(new user() { struserNo = "A2", struserName = "李四2", deposit = 40000, dtUser = null });
                list.Add(new user() { struserNo = "A2", struserName = "李四3", deposit = 40000, dtUser = null });
                list.Add(new user() { struserNo = "A3", struserName = "王五", deposit = 44000, dtUser = null });
               
                user u = null;
                double total = 0;
                String struserName = "";
                for (int i = 0; i < list.Count(); i++)
                {
                    u = list[i];
                    //1:取出所有存款帳號(struserNo)为“A1”的存款金額(deposit)之和
                    if (u.struserNo.Equals("A1"))
                    {
                        total += u.deposit;
                    }                //2:取出存款金額(deposit)为“40000”并且存款帳號(struserNo)为“A2”的存款名(struserName)
                    if (u.struserNo.Equals("A2") && (u.deposit == 40000)) 
                    {                    if (struserName.Equals(""))
                        {
                            struserName = u.struserName;
                        }
                        else {
                            struserName += " , " +  u.struserName;
                        }
                    }                //3,我也不会
                }                       Console.WriteLine(total);            Console.WriteLine(struserName);            Console.ReadLine();
            }
            class user
            {
                public string struserNo { get; set; }//存款帳號
                public string struserName { get; set; }//存款名
                public int deposit { get; set; }//存款金額
                public DataTable dtUser { get; set; }//一個user的DataTable
            }
        }
    }
      

  2.   


    1.var q = list.Where(o => o.struserNo == "A1").Sum(p => p.deposit);2.var q = list.Where(o => o.deposit == 40000 && o.struserNo == "A2").First().struserName;3.第三个没看懂
      

  3.   


    第二个这么写也可以
    var q = from a in list where a.deposit == 40000 && a.struserNo == "A2" 
               select new {uname = a.struserName};
      

  4.   

    1,2两个都是像我想像中的那样。谢谢!请问一下为什么第一句可以直接用Console.WriteLine(q);就能输出了结果:30000,而第二句则需要用            
                  foreach (var sss in q)
                {
                    Console.WriteLine(sss);
                }
    才能输出结果为:<uname = 李四>呢?第二句可不可以不用foreach就直接输出查询的结果为:李四呢?
      

  5.   


    因为 q 是集合嘛。。另外,关于问题3,List本身不带通知事件,可以换成BindingList<T>或者ObservableCollection<T>
    但从你目前的需求来看,这个DataTable应该是全局的,比如“张三”第2条加进去之后,DataTable里应该有2条记录。PS:已经有List<T> 何必再用DataTable呢?多此一举嘛
    static void Main(string[] args)
    {
        var list = new ObservableCollection<user>();
        list.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(users_CollectionChanged);
        list.Add(new user { struserNo = "A1", struserName = "張三", deposit = 10000, dtUser = null });
        list.Add(new user { struserNo = "A1", struserName = "張三", deposit = 20000, dtUser = null });    foreach (var u in list)
            Console.WriteLine(string.Join(",", u.dtUser.Rows[0].ItemArray));    Console.Read();
    }static void users_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            var user = (user)e.NewItems[0];
            if (user.dtUser == null)
            {
                user.dtUser = new DataTable();
                user.dtUser.Columns.Add("struserNo");
                user.dtUser.Columns.Add("struserName");
                user.dtUser.Columns.Add("deposit");
                user.dtUser.Rows.Add(user.struserNo, user.struserName, user.deposit);
            }
        }
    }
      

  6.   

    谢谢porschev和cai5,问题解决了,第三点在你们的答案提示中我也解决了。
      

  7.   

    fangxinggood又让我学习到新东西了~感谢!