Why is linq important
Extending this example, suppose we want to sort the results in reverse order of price, and also want the salesperson's name and number of purchased items in the final projection. Notice how naturally we can express these additional criteria without repetition:. An interesting point is that it's possible to transliterate the above SQL query back into LINQ, yielding a query that's every bit as repetitive - and at least as clumsy.
You'll often see typically non-working versions of such queries posted on forums - this happens as a result of thinking in SQL, rather than thinking in LINQ. Selecting from more than one table in SQL requires joining - the end result being rows of flat tuples.
If you've used SQL for many years, you may have become so accepting of this that it may not occur to you that this forced denormalization is often undesirable: it leads to data duplication and makes result sets awkward to work with on the client. In contrast, LINQ lets you retrieve shaped or hierarchical data. This avoids duplication, makes results easier to work with, and in most cases it even obviates the need for joining.
For example, suppose we want to retrieve a selection of customers, each with their high-value purchases. In LINQ, you can do this:. HighValuePurchases, here, is a collection. And because we were querying an association property, we didn't need to join. Which means the detail of whether this was a inner or outer join is nicely abstracted away. In this case, the query, when translated to SQL, would be an outer join: LINQ doesn't exclude rows just because a subcollection returns zero elements.
If we wanted something that translated to an inner join, we could do this:. LINQ also supports flat outer joins, adhoc joins, subqueries, and numerous other kinds of queries through a rich set of operators. What if we wanted to parameterize our previous example, so that the state "WA" came from a variable? This is all we do:. LINQ's parameterization is inline, typesafe, and highly readable. You may find it very helpful to run this sample under a debugger to observe how the Suits and Ranks methods execute.
You can clearly see that each string in each sequence is generated only as it is needed. Next, focus on how you're going to shuffle the cards in the deck.
The first step in any good shuffle is to split the deck in two. Place them underneath the foreach loop:. However, there's no shuffle method to take advantage of in the standard library, so you'll have to write your own.
The shuffle method you'll be creating illustrates several techniques that you'll use with LINQ-based programs, so each part of this process will be explained in steps. Briefly, an extension method is a special purpose static method that adds new functionality to an already-existing type without having to modify the original type you want to add functionality to.
Give your extension methods a new home by adding a new static class file to your program called Extensions. You can see the addition of the this modifier on the first argument to the method. That means you call the method as though it were a member method of the type of the first argument. That practice enables LINQ methods to be chained together to perform more complex queries. Naturally, since you split the deck into halves, you'll need to join those halves together.
In code, this means you'll be enumerating both of the sequences you acquired through Take and Skip at once, interleaving the elements, and creating one sequence: your now-shuffled deck of cards. The object returned by GetEnumerator has a method to move to the next element, and a property that retrieves the current element in the sequence.
You will use those two members to enumerate the collection and return the elements. This Interleave method will be an iterator method, so instead of building a collection and returning the collection, you'll use the yield return syntax shown above.
Now that you've written this method, go back to the Main method and shuffle the deck once:. How many shuffles it takes to set the deck back to its original order? To find out, you'll need to write a method that determines if two sequences are equal. After you have that method, you'll need to place the code that shuffles the deck in a loop, and check to see when the deck is back in order. Writing a method to determine if the two sequences are equal should be straightforward.
It's a similar structure to the method you wrote to shuffle the deck. Only this time, instead of yield return ing each element, you'll compare the matching elements of each sequence. When the entire sequence has been enumerated, if every element matches, the sequences are the same:.
This shows a second LINQ idiom: terminal methods. They take a sequence as input or in this case, two sequences , and return a single scalar value. When using terminal methods, they are always the final method in a chain of methods for a LINQ query, hence the name "terminal". You can see this in action when you use it to determine when the deck is back in its original order. Put the shuffle code inside a loop, and stop when the sequence is back in its original order by applying the SequenceEquals method.
You can see it would always be the final method in any query, because it returns a single value instead of a sequence:. Run the code you've got so far and take note of how the deck rearranges on each shuffle.
After 8 shuffles iterations of the do-while loop , the deck returns to the original configuration it was in when you first created it from the starting LINQ query. The sample you've built so far executes an out shuffle , where the top and bottom cards stay the same on each run.
Let's make one change: we'll use an in shuffle instead, where all 52 cards change position. For an in shuffle, you interleave the deck so that the first card in the bottom half becomes the first card in the deck.
The example below shows how you can use LINQ query with lambda expression to find a particular student s from the student collection. As you can see in the above example, we specify different criteria using LINQ operator and lambda expression in a single statement. Thus, LINQ makes code more compact and readable and it can also be used to query different data sources.
For example, if you have a student table in a database instead of an array of student objects as above, you can still use the same query to find students using the Entity Framework.
C ASP. Skill Tests ASP. LINQ allows usage of a single LINQ syntax while querying many diverse data sources and this is mainly because of its unitive foundation. LINQ offers the facility of joining several data sources in a single query as well as breaking complex problems into a set of short queries easy to debug. Previous Page. Next Page. Previous Page Print Page. Save Close.
Dashboard Logout.
0コメント