The popular answer is that LINQ is IN tegrated with C or VB , thereby eliminating the impedance mismatch between programming languages and databases, as well as providing a single querying interface for a multitude of data sources. While that's true, it's only part of the story. More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. SQL is a very old language—invented in Since then it's been extended endlessly, but never redesigned.
You might have become so accustomed to this that you can't see anything wrong! Let's take an example. You want to write a simple query that retrieves customers as follows:. That doesn't look too bad, right? But now suppose these results are feeding a web page, and we want to retrieve just rows Suddenly, you need a subquery :. Here's same query in LINQ. The gain in simplicity is clear:.
Only when we enumerate thirdPage will the query actually execute. You might have noticed another more subtle but important benefit of the LINQ approach.
We chose to compose the query in two steps—and this allows us to generalize the second step into a reusable method as follows:. The important thing, here, is that we can apply our Paginate method to any query. In other words, with LINQ you can break down a query into parts, and then re-use some of those parts across your application. Another benefit of LINQ is that you can query across relationships without having to join. Let's talk about hierarchical data. In the join example, we had two separate data sources movies and characters.
Sometimes we have everything in one object graph like so:. The good news is that Linq can help us to ask for data over the entire graph using an operator referred to as SelectMany. When we use our SQL like syntax however it looks a lot like the join statement.
Let's show it:. What we can see from the above is how we have two data sources orders and o. The first is the order itself and the other data source is the nested OrderItems on the order. Then we create a projection consisting of data from both an order , orderitem and even the product :. We've covered how the what and why of Linq and hopefully you saw the need for one unifying language regardless of the data source.
Furthermore, we've covered different operators in Linq and how you can use them to group , transform and compute what you need. It's impossible to cover everything so I urge you to have a look at the references section of this article to dive deeper. Hopefully, your understanding is now good enough so you can study further on your own. Learn how to use Linq in C and. If you are not using Linq when coding. NET - you are missing out. References More on joins in Linq There is more than one way to do joins, this should cover most scenarios.
The Problem Ok so Linq is supposedly changing everything. How exactly? Yea, I know, not the most fun work, but it needs to be done. What about it? There's that word again declarative and tell me about the data sources, I'm intrigued? Name , ch. Race ; foreach var c in obj. Type ; Console. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Language-Integrated Query LINQ is the name for a set of technologies based on the integration of query capabilities directly into the C language.
Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on.
With LINQ, a query is a first-class language construct, just like classes, methods, events. You write queries against strongly typed collections of objects by using language keywords and familiar operators. You don't have to use a for loop to find students using different criteria. For example, you can use the same delegate function to find a student whose StudentId is 5 or whose name is Bill, as below:.
The C team felt that they still needed to make the code even more compact and readable. So they introduced the extension method, lambda expression, expression tree, anonymous type and query expression in C 3. You can use these features of C 3. The example below shows how you can use LINQ query with lambda expression to find a particular student s from the student collection.
0コメント