3 ms·
What i mostly like about LINQ is IQueryable. It's awesome! I just design the IQueryable with if's, switches, ... and when i need to execute it. I use .ToList()
by NicoJuicy 10y ago
What i mostly like about LINQ is IQueryable. It's awesome!
I just design the IQueryable with if's, switches, ... and when i need to execute it. I use .ToList()
The IQueryable is then translated to SQL and executed it.
- NicoJuicy 10y agoFollow up on my usage on Linq ( fyi) Now i design most of my pages through 1 service/method with a lot of if's and else and re-use that method all the time For example, a ecommerce-site i made has one function for querying the products, paging, searching, sorting by X, ...). Although, it get's rather complex lately, i wouldn't have it any other way. ( with complex, i mean i now also grouping them by category and category code, a category order number, a product order number, by product title ( varying by the current language), ... And yes, it' seperated in my service layer ;) and no, i won't change it because this is the fastest way untill now. Alternatively, you could use Extensions on IQueryable and IOrderedQueryable. Then you do something like db.Products.FilterBySearchTerm(searchTerm).OrderAllBy(sortPropertyName); But i like ProductHelper.GetProducts(filterWithSettings,SortOnSettings,GroupBySettings); more, mostly because it's easier when i need to customize all the methods because of some "sudden" Change Request that changes all the internal workings :( Also, if you are new to Linq To Sql. Don't forget DbFunctions / SqlFunctions for built-in SQL Functionality ( depending the EF version you are using). Eg. The LIKE operator has been added in the new EF. Also, you can always using DataContext.Database.Sql() for using standard SQL. ===== Pro Tip: DbContext is already a Repository Pattern if you are using DDD ! ===== PS. If you don't know LINQ. It generates a long SQL query and it's a bit slow, BUT it's very productive in development . Here's an example with both Generated SQL (commented between /* */ ) and Linq in one of my subbmitted issues on EF : https://github.com/NicoJuicy/EF-ContainsAndStartsWith/blob/master/EF-ContainsAndStartsWith.Test/UnitTest1.cs https://github.com/NicoJuicy/EF-ContainsAndStartsWith/blob/m... I like LINQ: Develop fast and improve when something executes slow :)
- OldSchoolJohnny 10y agoAnd then time goes by some guy comes along rips out the LINQ and fixes all that so it's performant under load with hand crafted sql queries.
- NicoJuicy 10y agoAnd then you have no more errors in the VS Error Window ( LINQ is static typed), when someone changes the domain layer and everything goes to * when going into production and now you have to manually check all strings if they are adjusted.. As said before, change to pure SQL WHEN something goes too slow. You don't have to change everything. It will bite you in the * later ;) Gotta love static typed :)