Quantcast
Channel: VITO小舖 - ASP.NET
Viewing all articles
Browse latest Browse all 41

資料格式轉換

$
0
0

LINQ 不僅僅可以用來查詢資料, LINQ 也允許我們將來源資料轉換成其他格式的資料。例如轉成 XML 或 JSON 格式,亦或是自訂型別都可以。

Mapping to Another Type or Anonymous Object

LINQ allows you to use the Select keyword to push your results to an instance of a strongly typed object or even a new, anonymous type.

Using select new to transform to another type


private class MyUser
{
public string Name { get; set; }
public string ID { get; set; }
}
protected void ToAnonymousObject_Click(object sender, EventArgs e)
{
var employees = TestData.GetEmployeeList();

// Transform to Custom Type
var empQuery = from emp in employees
select new MyUser
{
Name = emp.FirstName + " " + emp.LastName,
ID = emp.EmployeeId
};

// Transform to Anonymous Type
var empQuery2 = from emp in employees
select new
{
Name = emp.FirstName + " " + emp.LastName,
ID = emp.EmployeeId
};
}

Merging Multiple Data Sources

LINQ also lets you merge two similar datasets by using the Concat method from the list object.


ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"];
NorthwindContext nwdb = new NorthwindContext(connString.ConnectionString);

Table Employees = nwdb.GetTable();

var empQuery = (
from emp in Employees
where emp.City == "Seattle"
select emp
)
.Concat
(
from emp in Employees
where emp.City == "London"
select emp
);

Performing Operations on Results

You can perform mathematical and string operations on your result sets.


var employees = TestData.GetEmployeeList();

var empQuery = from emp in employees
select new
{
Description =
string.Format("I'm {0} {1}, {2} years old.", emp.FirstName, emp.LastName, emp.Age)
};

foreach (var user in empQuery)
{
myDebug.WriteLine(user.Description);
}
//I'm Andrew Fuller, 40 years old.
//I'm Apple Chen, 10 years old.
//I'm Banana Shao, 18 years old.
//I'm Melon Lai, 30 years old.

Transforming Results into XML

You can use LINQ to XML to transform data from a LINQ query into an XML structure. You do so by using the XElement class and the LINQ Select keyword to create new elements nested inside one another like an XML tree.


var employees = TestData.GetEmployeeList();

var empXml = new
XElement("Employees", from emp in employees
select new XElement("Employee",
new XElement("Id", emp.EmployeeId),
new XElement("Name", emp.FirstName + " " + emp.LastName),
new XElement("Age", emp.Age)
)
);

//
//
// 987654321X
// Andrew Fuller
// 40
//

//
// 1234567890
// Apple Chen
// 10
//

//
// 1234567892
// Banana Shao
// 18
//

//
// 1234567891
// Melon Lai
// 30
//

//

Transforming Results into JSON

Given the increased use of JQuery and AJAX, you might need to transform your LINQ query results into JSON format for working with these client-side libraries.

This serializer can be found in the System.Runtime.Serialization.Json namespace. The actual class name of the serializer is DataContractJsonSerializer . It can be used to serialize types and lists of types that are marked as DataContract .


[DataContract]
public class TestEmployee
{
[DataMember(Order = 0)]
public string EmployeeId { get; set; }
[DataMember(Order = 1)]
public string FirstName { get; set; }
[DataMember(Order = 2)]
public string LastName { get; set; }
[DataMember(Order = 3)]
public int Age { get; set; }
public TestEmployee(string id, string firstname, string lastname, int age)
{
EmployeeId = id;
FirstName = firstname;
LastName = lastname;
Age = age;
}
}

Using the WriteObject method of DataContractJsonSerializer class to write the query results to a MemoryStream.


var employees = TestData.GetEmployeeList();

var empJson = from emp in employees
where emp.Age > 30
select emp;

DataContractJsonSerializer ser =
new DataContractJsonSerializer(typeof(IEnumerable));

MemoryStream ms = new MemoryStream();

ser.WriteObject(ms, empJson);
string json = Encoding.Default.GetString(ms.ToArray());
ms.Close();
Response.Write(json);

//[{"EmployeeId":"987654321X","FirstName":"Andrew","LastName":"Fuller","Age":40}]


Viewing all articles
Browse latest Browse all 41

Trending Articles