2012年9月6日 星期四

C#: LINQ to Object 和 LINQ to XML 超好用之一


假設有此一 XML

<?xml version="1.0encoding="utf-8?>
<
customers>
    <
customer>
        <
customerid>ALFKI</customerid>
        <
city>Berlin</city>
        <
age>20</age>
    </
customer>
    <
customer>
        <
customerid>BONAP</customerid>
        <
city>Marseille</city>
        <
age>21</age>
    </
customer>
    <
customer>
        <
customerid>CONSH</customerid>
        <
city>London</city>
        <
age>30</age>
    </
customer>
    <
customer>
        <
customerid>EASTC</customerid>
        <
city>London</city>
        <
age>34</age>
    </
customer>
    <
customer>
        <
customerid>FRANS</customerid>
        <
city>Torino</city>
        <
age>35</age>
    </
customer>
    <
customer>
        <
customerid>LONEP</customerid>
        <
city>Portland</city>
        <
age>40</age>
    </
customer>
    <
customer>
        <
customerid>NORTS</customerid>
        <
city>London</city>
        <
age>25</age>
    </
customer>
    <
customer>
        <
customerid>THEBI</customerid>
        <
city>Portland</city>
        <
age>36</age>
    </
customer>
</
customers>



還有此一物件
public string CustomerID { get; set; }
public string City { get; set; }
public int Age { get; set; }
public static IEnumerable<Customer> CreateCustomers()
{
   return new List<Customer>
   {
      new Customer { CustomerID = "ALFKI", City = "Berlin", Age=20   },
      new Customer { CustomerID = "BONAP", City = "Marseille" , Age=21},
      new Customer { CustomerID = "CONSH", City = "London", Age=30    },
      new Customer { CustomerID = "EASTC", City = "London", Age=34    },
      new Customer { CustomerID = "FRANS", City = "Torino", Age=35    },
      new Customer { CustomerID = "LONEP", City = "Portland", Age=40  },
      new Customer { CustomerID = "NORTS", City = "London" , Age=25   },
      new Customer { CustomerID = "THEBI", City = "Portland", Age=26  }   
   };
 }
}

使用方法範例一:把物件理的值拿來做dropdown list
抓取想要的值,可以拿來做dropdown List 來用
public static List<Customer> GetCities()
{
     var customers = from customer in Customer.CreateCustomers()
                     orderby customer.City
                     select new Customer { City = customer.City };
     return customers.ToList();
}



使用方法範例二:XML來源放到物件裡
我喜歡這方法,我常有一些 需要讀取 web service XML。以前用 XML DOM又慢又沒效率又複雜,又不美觀。。。

public static List<Customer> GetCustomerFromXML(string city)
{
    XDocument xmlDoc = XDocument.Load("CustomerXML.xml");
    var customers = from customer in xmlDoc.Descendants("customer")
                    where customer.Element("city").Value == city
                    select new Customer
                    {
                        CustomerID = customer.Element("customerid").Value,
                        City = customer.Element("city").Value,
                        Age = Convert.ToInt32(customer.Element("age").Value)
                    };
    return customers.ToList();
}
使用方法範例三:從現有List <T>中取出符合條件的<T>
public static List<Customer> GetCustomersFromObject(string city)
{
     var customers = from customer in Customer.CreateCustomers()
                     where customer.City == city
                     select new Customer
                     {
                         CustomerID = customer.CustomerID,
                         City = customer.City,
                         Age = customer.Age
                     };
     return customers.ToList();
 }





沒有留言:

張貼留言