Linq to SQL vs Link to XML RRS feed

  • Question

  • Dear all,

    I was wondering what is the difference behind the scene when using Linq to SQL or Link to XML.

    What I mean and understand is that Linq to SQL behind the scene translate theLINQ querry syntax to SQL statment but what happen to Linq to XML ?

    In other words, by using LINQ, what is the magic behind the scene that it work either with SQL or XML or Object, with exactly same syntax ?

    Thanks for that


    Thursday, October 12, 2017 12:12 PM

All replies

  • LINQ to XML is LINQ-to-Objects which work on objects representing XML. When user writes a LINQ query to retrieve nodes from an XDocument, user is actually invoking extension methods that returns IEnumerable<T>, and invoking the standard query operators as they are defined for IEnumerable<T>. So LINQ-to-XML mostly consists of the XElement family of XML-representing classes and the many extension methods that are provided for those, in order to make queries easier.

    LINQ to XML works as follows:

    • Load XML into memory in a variety of ways (file, XmlReader, and so on).
    • Create an XML tree from scratch.
    • Insert new XML Elements into an in-memory XML tree.
    • Delete XML Elements out of an in-memory XML tree.
    • Save XML to a variety of output types (file, XmlWriter, and so on).

    LINQ to SQL is an Object Relational Mapper (ORM). LINQ to SQL provides a Data Access Layer that uses a pre-defined object model that maps your database to strongly typed classes.

    LINQ to SQL works as follows:

    • Needs a Data Context object. The Data Context object is the bridge between LINQ and the database. LINQ to Objects doesn’t need any intermediate LINQ provider or API
    • Returns data of type IQueryable<T> while LINQ to Objects returns data of type IEnumerable<T>
    • LINQ to SQL is translated to SQL by way of Expression Trees, which allow them to be evaluated as a single unit and translated to the appropriate and optimal SQL statements. LINQ to Objects does not need to be translated
    • LINQ to SQL is translated to SQL calls and executed on the specified database while LINQ to Objects is executed in the local machine memory

    Thanks, AT

    • Edited by Aishwarya Tripathi Thursday, October 12, 2017 1:09 PM Spacing adjusted
    • Proposed as answer by Bob Ding Monday, October 16, 2017 9:00 AM
    Thursday, October 12, 2017 1:08 PM
  • Thnaks for the details explaination.

    Linq to XML compare to Link to SQL, which one is faster in terms of performance and why ?

    Friday, October 13, 2017 8:14 AM
  • Hi,

    This is a question does not have exactly answer since there is not a clear answer it is subjective.

    It's no comparison that they're not the same starting point.

    What you need to consider is which one you should use to make your data persistence, Database or XML?

    If you have a simple data structure with not large data, and  the data changes less, you can take xml to store data. Otherwise you'd better use the database. Of course,this ultimately depends on your project requirements.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Bob Ding Monday, October 16, 2017 8:57 AM
    • Proposed as answer by Bob Ding Monday, October 16, 2017 9:01 AM
    Monday, October 16, 2017 8:56 AM