Timeline for Is my LINQ query safe and efficient?
Current License: CC BY-SA 3.0
15 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 15, 2016 at 12:23 | history | migrated | from stackoverflow.com (revisions) | ||
Jun 13, 2016 at 1:13 | comment | added | Robert McKee | @TedMarley If you are doing loads of join(), then you doing it wrong. You should have your navigation properties set up, and then you never do any joins at all. | |
Jun 12, 2016 at 23:58 | comment | added | Ted Marley | yeah i have been reading a few other articles/questions and it seems do which ever one best suits the query you need. Both are best practices so i guess if its loads of join() then just use a query syntax due to how unreadable the method syntax looks. - Thanks for all your advice @ManOVision i feel like i can be a linq expert with some good practice now. Bit difficult to get your head round when you first look at the lambda expressions but when you get it you get it (like a lightbulb turns on)! Cheers :D | |
Jun 12, 2016 at 23:55 | comment | added | ManOVision | @TedMarley You asked if you should always use lambda statements and there is no set rule. First or Single will execute a TOP 1 so lambda here is common. You can use them on nearly any LINQ method and combine them such as Where(lambda).FirstOrDefault() to do the same thing. Where returns an IQueryable and FirstOrDefault will actually execute so they can be chained like this. Start with a simple dataset and test all the LINQ methods and you'll start to see where the lambda fits better or not. | |
Jun 12, 2016 at 23:50 | comment | added | Ted Marley | i don't want to go back already literally so much cleaner and so much less lines of code then having to do a sqlconnection then sqlcommand then the actually SQL query and then loop through the data! LinQ is what dreams are made from why have i just started this!! haha thanks a lot @ManOVision | |
Jun 12, 2016 at 23:47 | comment | added | ManOVision | Thanks cFrozenDeath for mentioning lazy loaded vs executed. @TedMarley, queries that result in an IQueryable aren't executed against the database until iterated. This helps a lot with not loading datasets until needed. You can also combine statements together such as search terms and then only execute the query when needed. Whole books are written on this this. Have fun with this, it's such a difference from executing raw T-SQL. You'll never want to go back. | |
Jun 12, 2016 at 23:38 | comment | added | user92754 | @TedMarley I forgot to mention that the delayed executed query it's not because how you construct your LinQ (query syntax (yours) or method syntax (answer)), but because the FirstOrDefault method instructs LinQ to perform the query. See this post programmers.stackexchange.com/questions/83057/… for your last question | |
Jun 12, 2016 at 23:36 | vote | accept | CommunityBot | ||
Jun 12, 2016 at 23:36 | comment | added | Ted Marley | should you always use lambda expressions like the example given by manovision as mine seems a little strange when comparing it against this one.. @cFrozenDeath | |
Jun 12, 2016 at 23:34 | comment | added | Ted Marley | ahhhhh @cFrozenDeath thats makes so much sense now!! thanks for explaining that! | |
Jun 12, 2016 at 23:32 | comment | added | user92754 | @TedMarley your version represents a delayed executed query, which means you are not asking for it to run immediately. First will throw an Exception if the query returns no elements, FirstOrDefault will return default(T) (null or the Struct default) instead of throwing the exception. FirstOrDefault won't give you an IEnumerable(T) however, just T. | |
Jun 12, 2016 at 23:28 | comment | added | Ted Marley | my query does still get the data back @cFrozenDeath.. all held in user object.. so by execute you mean actually performing the query or..? | |
Jun 12, 2016 at 23:26 | comment | added | Ted Marley | Thanks for your answer that does look a lot cleaner.. in terms of FirstOrDefault will that return null if no customer is found - this is nice as you don't have to use is .any() all the time? so is kind of like a TOP 1 SQL query? | |
Jun 12, 2016 at 23:24 | comment | added | user92754 | You should also explain that your code executes the query, while the OP's code doesn't, and why. | |
Jun 12, 2016 at 23:19 | history | answered | ManOVision | CC BY-SA 3.0 |