Add LINQ To Your Software Development Toolkit
Add LINQ To Your Software Development Toolkit
Maybe, I am behind the curve but I have slowly been getting into LINQ and I have to admit that I like it. I can see myself becoming an addict. If you're not a LINQ addict, you might wonder what the fuss is about. SQL isn't broken, so why fix it? Why do we need another querying language? From What I can see LINQ is much cleaner and at a higherlevel. There "might" be times when it is better to work with SQL I suppose, but in most situations, working in a modern tidy language and not having to worry about lower-level details is a big win. SQL is super old and can get quite messy. Queries can be very complicated to write at times. For example, look at the SQL code below:
SELECT TOP 10 UPPER (c1.Name) FROM Customer c1 WHERE c1.Name LIKE 'A%' AND c1.ID NOT IN ( SELECT TOP 20 c2.ID FROM Customer c2 WHERE c2.Name LIKE 'A%' ORDER BY c2.Name ) ORDER BY c1.Name
Not only is this complicated and messy, but it violates the DRY principle (Don't Repeat Yourself). Here's same query in LINQ. The gain in simplicity is clear: var query = from c in db.Customers where c.Name.StartsWith ("A") orderby c.Name select c.Name.ToUpper(); Another benefit of LINQ is that you can query across relationships without having to join. For instance, suppose we want to list all purchases made by customers who live in Washington, whose purchased items exceed $1000 in total value. This requires joining across 4 tables (Purchase, Customer, Address and PurchaseItem). In LINQ, the query is effortless: from p in db.Purchases where p.Customer.Address.State == "WA" where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000 select p No joining, grouping or messy subqueries. Extending this example, suppose we want to list the purchases in reverse order of value, and we want to include the salesperson's name and number of purchased items in the final projection: from p in db.Purchases where p.Customer.Address.State == "WA" let purchaseValue = p.PurchaseItems.Sum (pi => pi.SaleAmount) where purchaseValue > 1000 orderby purchaseValue descending select new { p.Description, p.Customer.SalesPerson.Name, PurchaseItemCount = p.PurchaseItems.Count() } Again, we have a clean, typesafe and composable query that follows the DRY principle. My point is that if you want to write better queries and you want to do it quicker, you should look into LINQ. Despite its power, LINQ doesn't deprecate SQL. It takes more than 95% of the querying brunt, but you still sometimes need SQL for: * Hand-tweaked queries (especially with optimization or locking hints) * Queries that involve selecting into temporary tables, then querying those tables * Predicated updates and bulk inserts And of course you still need SQL for triggers. (SQL's also needed for stored procedures and functions, although the need for these crops up less often when you're using LINQ). You can combine LINQ with SQL by writing table-valued functions in SQL, and then calling those functions within more elaborate LINQ queries. Having to know two querying languages is not really an issue because you'll want to learn LINQ anywayLINQ is so useful for querying local collections and XML DOMs. If you're still using the old XmlDocument-based DOM, you'll find LINQ to XML's DOM a dramatic step up. And because LINQ is easier to master than SQL, the goal of writing really good queries is more achievable with LINQ than with SQL.
Cell Phone Spying Software SpyBubble Examine How to Fix Word Program Errors What Causes Program Errors? Bettering Your Company As A Result Of Cell Phone Spying Software Program How to Fix Runtime Error 1004 How to Fix Runtime Error 429 How to Fix Runtime Error 424 How to Fix Runtime Error 339 Different Software Development Methodology in Practice How Is FxDialogue's Innovative Trading Software Helpful? What is an Ehr software? What is the difference between EHR and EMR software? The Features Of Photo Editing Software
www.yloan.com
guest:
register
|
login
|
search
IP(3.144.230.138) /
Processed in 0.008557 second(s), 7 queries
,
Gzip enabled
, discuz 5.5 through PHP 8.3.9 ,
debug code: 6 , 3460, 95,