LINQ Flashcards

1
Q

What is a LINQ query?

A

LIQN allows you to query data from different types of data using a single syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the interface types that LINQ can query?

A

Link can query any IEnumerable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the basic structure of a LINQ query?

A

var myQuery = from user in users where user.contains(“m”) ordery by user.Length ascending select user;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When will the LINQ query be executed?

A

When any operation that demands the fetch be executed (count, toList() and etc).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to group results using LINQ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does LINQ stand for?

A

Stands for Language Integrated Query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How interpolate strings in C#?

A

By using var myString = $”The count of users is: {users.Count}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What was the need behind LINQ before it was invented?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How the sort was implemented before LINQ?

A

By implementing your own class that implements IComparer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to get just the fist five items of a list?

A

By using the .Take(5) method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is there one way to invoke a LINK query?

A

No… you can also call simplified methods. such as: myList.OrderByDescending(l => l.myValue).Take(5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the LINQ query syntax?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why does the query syntax starts with from and ends with select?

A

The language designers made this decision to increase the support of Intelisense once it is upfront known what the type the query is.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the extension method syntax?

A

Is when you filter, order and do other operations using methods only. list.orderby…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are all extension methods available for LINQ syntaxes (query and method)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Will LINQ try to optimize queries that are actually fired against a database?

A

Yes.

17
Q

What is the yield statement? Why is it used in LINQ queries?

A

It is a behavior called deferred execution. Used in LINQ queries due to performance.

18
Q

How the yield/deferred execution behaves?

A

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.

19
Q

Where the deferred execution would shine?

A

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).

20
Q

What is an easy way to notice when a LINQ method (and any other?) uses deferred execution?

A

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.

21
Q

What would be a good practice when you need to iterate many times over the query?

A

You can use the ToList() and use that to manipulate the objects (or even call .Count())

22
Q

What LINQ queries and the use of deferred execution causes to the “normal” exception try..catch flow?

A

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.

23
Q

What the order by causes in the in memory structures, mainly when you have thousands of items?

A

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.

24
Q

Where to find the documentation about what is deferred and what is not?

A

just google for “Classification of Standard Query Operators by Manner of Execution (C#)”.

25
Q

Why is deferred streaming execution so fast and secure to use?

A

Because it can process an unlimited amount of items in a very fast manner and if you combine it with things such as Take(10), even millions of rows can be “processed” very fast.

26
Q

Can link be used to query data from files?

A

Yes. e.g File.ReadAllLines(fileName).

27
Q

What is the so called projection?

A

Is the Select method. Means you’re transforming one kind of data and turning into a subset of the data or a different data. When the select is not explicit, it returns the “from” data;

28
Q

How to make two order by using LINQ extension methods? How to do the same using LINQ query?

A

OrderBy(e => e.Name).ThenByDescending(e => e.Age). Link query you just need to add comma, order by e.Name, e.Age descending.

29
Q

What is the difference between take(1) and First extension methods?

A

The Take(1) will be deferred executed and the First() will be executed right away. Also, First returns the object itself, not an IEnumerable.

30
Q

What is the difference between first and firstordefault?

A

First will simply return the first item and if list is empty it’ll throw an exception. FirstOrDefault will return the default value of the given type, e.g: int will be 0 but a class would still be null.

31
Q

What is the purpose of the Any method?

A

When called empty, it checks if the list is empty. Can also check if the list contain a specific entry by delegating a lambda.

32
Q

Can the project (aka transformation) be delegated? Can we create anonymous types as well?

A

Yes. You can delegate normally using extension methods. Yes, can return anonymous types.

33
Q

What happens when you create an anonymous type and do not define prop names, just the values?

A

The prop names of the anonymous type will match the ones from the source values.

34
Q

What does the SelectMany do?

A

It allows iterating in a sequence inside a sequence… might be useful for aggregating data.