LINQ Flashcards
(34 cards)
What is a LINQ query?
LIQN allows you to query data from different types of data using a single syntax.
What are the interface types that LINQ can query?
Link can query any IEnumerable
What is the basic structure of a LINQ query?
var myQuery = from user in users where user.contains(“m”) ordery by user.Length ascending select user;
When will the LINQ query be executed?
When any operation that demands the fetch be executed (count, toList() and etc).
How to group results using LINQ?
var userQuery = from user in users group user by user.length into userGroup select userGroup. Then it is necessary to loop in the user group and use userGroup.Key in order to get the value of the group aggregator.
What does LINQ stand for?
Stands for Language Integrated Query.
How interpolate strings in C#?
By using var myString = $”The count of users is: {users.Count}”
What was the need behind LINQ before it was invented?
Each type of collection needed its own query mechanism, then LINQ purpose is to integrate them all into a single mechanism. types of common collections: object data, relational data and xml data.
How the sort was implemented before LINQ?
By implementing your own class that implements IComparer.
how to get just the fist five items of a list?
By using the .Take(5) method.
Is there one way to invoke a LINK query?
No… you can also call simplified methods. such as: myList.OrderByDescending(l => l.myValue).Take(5)
What is the LINQ query syntax?
Is the syntax that looks very similar to a regular SQL query. Always starts with the from keyword. Similar to for in syntax. Always end with the select keyword.
Why does the query syntax starts with from and ends with select?
The language designers made this decision to increase the support of Intelisense once it is upfront known what the type the query is.
What is the extension method syntax?
Is when you filter, order and do other operations using methods only. list.orderby…
Are all extension methods available for LINQ syntaxes (query and method)?
No. method syntax has a few more methods. Usually when you want to use, count, take and skip you’ll go for the method syntax.
Will LINQ try to optimize queries that are actually fired against a database?
Yes.
What is the yield statement? Why is it used in LINQ queries?
It is a behavior called deferred execution. Used in LINQ queries due to performance.
How the yield/deferred execution behaves?
It is able to return any item of a IEnumerable or IEnumerator right away when it is called even when there are more stuff to iterate over, meaning that the caller will have something to process already until the loop where is raising yields finishes.
Where the deferred execution would shine?
Author says it helps a lot operators such as Take(10) once it’ll not loop through everything.
IMO: Would shine for heavy processes where each item would get processed right away (and trigger some sort of event?) and probably even more when using async/await (e.g spawn a task for each element in the list).
What is an easy way to notice when a LINQ method (and any other?) uses deferred execution?
If you ctrl+. the where method, you’d see it returns an enumerable of T (this is very likely to be deferred). When you ctrl+. and see an IEnumerable of the concrete type, such as ToList(), then it is likely to be NOT-DEFERRED.
What would be a good practice when you need to iterate many times over the query?
You can use the ToList() and use that to manipulate the objects (or even call .Count())
What LINQ queries and the use of deferred execution causes to the “normal” exception try..catch flow?
Try catching the query itself is not effective once it will not fetch the real data. Therefore, you need to place the try catch in the places where the fetch actually happens.
What the order by causes in the in memory structures, mainly when you have thousands of items?
It makes the deferred execution to not work properly once it HAS to fetch everything in the beginning. It is very likely to cause performance issues. Filter are very helpful here.
Where to find the documentation about what is deferred and what is not?
just google for “Classification of Standard Query Operators by Manner of Execution (C#)”.