1

I'm curious how exactly the Enity Framework integrates with LINQ in order to generate SQL statements to run against the database.

So lets say I have my own custom collection object. How do you integrate that object with LINQ to generate SQL statements?

Kratz
  • 4,280
  • 3
  • 32
  • 55
  • 1
    This is a pretty broad question. Can you be more specific? – Justin Morgan - On strike Aug 05 '11 at 19:40
  • @Justin, it's just that the answer is potentially a very deep subject. I don't know much about the implementation of the interfaces, so I can't really ask a specific question. I'm more or less just looking for where you start with such a task. – Kratz Aug 05 '11 at 20:03

3 Answers3

3

If you wanted to make your own collection that created SQL statements, then what you would do is to have your collection class implement the IQueryable interface. As part of this interface, when the Linq expression is executed, the .NET framework will pass a Linq Expression Tree to your custom implementation of IQueryable. Your code would then parse this expression tree, and generate the SQL as needed, or do whatever other actions are needed, and return the result.

Edit: Adding Links

Community
  • 1
  • 1
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • 1
    Good answer, +1, but you can improve it by giving some links on futher readings. Personally I would recommend to start with Jon Skeet's book and then to continue reading further about implementing IQueryable if you would like to implement it in your code. – Dmitrii Lobanov Aug 05 '11 at 20:15
  • @Dmitry - Links added, as requested. Enjoy! – CodingWithSpike Aug 05 '11 at 20:26
2

You have to implement certains interfaces (IQueryable for example) over your custom class in order to use Linq to query over them. Also, if you have a collection composing your class, then you can expose its Enumerator in order to gain in this terrain (Implement IEnumerable). See: How to implement IQueryable

If you need a much specialized "Linq" functionality, like the one that is used in Linq to SQL to translate a Linq expression to T-SQL, then Linq is like a standard: Diferent set of technologies implement Linq diferently, but with certain guidelines. That is correct for Linq to XML, Linq to Entities, Linq to SQL, etc. See:

Walkthrough: Creating an IQueryable LINQ Provider

SalvadorGomez
  • 552
  • 4
  • 15
  • I don't exactly know what you mean "in order to have Linq support." I just want to make sure people don't get the wrong impression.. LINQ stands for Language Integrated Query and works on objects right out of the box without needing to decorate existing objects. – Quintin Robinson Aug 05 '11 at 19:49
  • @Quintin - I think he's referring to the fact that you need to implement interfaces such as `IEnumerable` or `IQueryable` to get access to most of the LINQ extension methods. It doesn't necessarily apply to single instances of a given class. However, this is only a small part of the huge picture that is LINQ. The question is very broad. – Justin Morgan - On strike Aug 05 '11 at 19:57
  • @Quintin, Justin, You're both right. I've edited my answer. Thanks – SalvadorGomez Aug 05 '11 at 22:06
1

To help anyone out there with similar requirements, I've written a series of tutorials on how to create a simple LINQ provider, parse expression trees, convert them into the desired output (e.g. SQL) and compile. Having developed one such provider for a production system, I eventually thought the experience might help someone else, with the challenge being to find the time to put it into writing. I've greatly benefited in the past from others sharing their knowledge, especially on subjects where it is scarce, so, hopefully, this may also find an audience: