IEnumerable<Customer> cust = new []
{
new Customer{ Id = 1, Name = "Test" }
};
จะลองทดสอบสร้าง Collection เก็บข้อมูล User แล้ว Input ที่เข้ามาให้ไปตรวจสอบกับข้อมูลใน Collection ยังไงว่ามีอยู่จริง
Tag : .NET
Date :
2014-11-03 21:12:32
By :
kenghockey
View :
1531
Reply :
4
No. 1
Guest
Code (C#)
class Program
{
static void Main(string[] args)
{
viewcustomer vcust = new viewcustomer();
Console.WriteLine(vcust.getCustomerName(1));
Console.WriteLine(vcust.getCountCustomer());
Console.ReadKey();
}
}
public class customer
{
public int id { get; set; }
public string name { get; set; }
}
public class viewcustomer
{
private IEnumerable<customer> listcustomer = new[] {
new customer { id = 1, name = "test1" }
,new customer { id = 2, name = "test2" }};
public string getCustomerName(int id)
{
string result = "";
result = listcustomer.Where(c => c.id == id).FirstOrDefault().name.ToString();
return result;
}
public int getCountCustomer()
{
return listcustomer.Count();
}
}