To transform a LINQ query into an object, you need to serialize the query result into an object. Serialization refers to the process of converting an object into a stream of bytes that can be stored in memory, a database or a file.
One way to achieve this is by using the DataContractSerializer class which is available in the System.Runtime.Serialization namespace. This serializer can convert objects to and from XML.
Here is an example of how to serialize a LINQ result in C#:
main.cs1072 chars43 linesIn this example, we have created a Person class with DataContract and DataMember attributes which tell the DataContractSerializer how to serialize and deserialize the object. We then created a list of Person objects and performed a LINQ query to get all people older than 30.
We then created a DataContractSerializer object and used it to serialize the LINQ query result into an XML stream which we then read into a string and printed to the console.
Note: This is just one way to transform LINQ into an object. There are many other serialization techniques and libraries available in C# that you can use depending on your use case.
gistlibby LogSnag