日期:2014-05-18  浏览次数:20824 次

C# Linq操作List的問題,求教!謝謝~
初学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要如何作修改才能达到第三点要求?)

------解决方案--------------------
C# code
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))