111,126
社区成员
发帖
与我相关
我的任务
分享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
}
}
}
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);
}
}
}