LINQ Flashcards

(28 cards)

1
Q

What is linq . Why linq

A

LINQ, or Language Integrated Query, gives a unified syntax to express queries in a declarative and readable way.

With LINQ, you can query in-memory objects, databases, XML, or other abstractions using the same style. It supports method syntax and query syntax.

LINQ uses deferred execution (also called lazy execution), so results are only fetched when you iterate. You can compose queries step by step and plug different providers (like LINQ to Objects, LINQ to SQL, LINQ to XML). This makes code simple, powerful, and consistent.

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

Why linq

A

We use LINQ (Language Integrated Query) because it gives a unified, readable, declarative syntax to query in-memory data, databases, or XML. It supports lazy (deferred) execution, lets you compose queries, and works with different providers using the same style.

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

IEnumerable vs IQUERYABLE

A

IEnumerable works with in-memory collections. It uses deferred (lazy) execution but filtering happens in your application after data is loaded. Good for small datasets.
Linq to objects

IQueryable works with remote data sources like a database. It translates the LINQ expression into a provider-specific query (like SQL) so filtering happens at the source. Better for large datasets.
Linq to entity

✅ Example:

IEnumerable → loads all customers, then filters in memory.

IQueryable → sends the filter to SQL, so only matching customers are fetched.

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

Deferred execution . Why

A

Deferred execution means a LINQ query is not run at declaration. It builds an execution pipeline and runs only at enumeration.

Why:

Allows query compose step by step.
Supports infinite sequences.
Reevaluates captured variables each time you enumerate.

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

Immediate vs deferred

A

Deferred Execution

What happens: Query is not run immediately. It builds a pipeline and runs only when enumerated. Each enumeration reevaluates the data, reflecting changes made after query definition.
When to use: When you want to compose queries, work with large or changing datasets, or need lazy evaluation.
Examples: Where(), Select(), Take(), Skip()

Immediate Execution

What happens: Query is executed immediately and results are stored. Later changes to the source do not affect the result.
When to use: When you need fixed results, want to force evaluation, or avoid multiple enumerations over expensive queries.
Examples: ToList(), ToArray(), Count(), First(), Sum()

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

Materialize

A

Materialize in LINQ means forcing a query to execute immediately and storing its results in memory.

What happens: Deferred query is evaluated, and results are captured in a collection.

Why: To freeze the data, avoid multiple enumerations, or work with a snapshot of changing data.

Common methods: ToList(), ToArray(), ToDictionary().

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

When not to use linq

A

Avoid LINQ when performance is critical, queries are complex and translate inefficiently to SQL, or you need side-effects like inserting or more control like index access to form logic. Also, materializing large queries can use extra memory, so plain loops may be better for tight or memory-sensitive operations.

——-——————–————
When performance is critical: LINQ can add overhead for very large datasets or tight loops.

When complex queries generate inefficient SQL: With IQueryable, some LINQ expressions translate poorly to database queries.

When side-effects are needed: LINQ is declarative and avoids modifying data during queries.

When memory is limited: Deferred execution can still keep references alive; materializing large queries consumes memory.

When low-level control is required: Some operations need manual loops for precise control or early exits.

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

Empty sequence any all defaultifempty

A

Any gives false because nothing satisfy

All gives true because there is no element that fails

DefaultIfEmptry gives ienumerable with one element that is the type Default or null for reference types

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

Count>0 vs Any
Where.any
Select.any

A

Any better

Count() > 0: Enumerates the entire collection to get the count, then checks if it’s greater than zero. Slower for large datasets.

Same for where.any, select.any

Any(): Stops as soon as one element is found, faster and more efficient.

✅ Use Any() when you just want to check for existence.

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

Where.first vs first
Tolist.first vs first

A

Where(…).First(): Filters the collection first, then returns the first matching element. Internally creates an intermediate filtered sequence.

First(…): Directly returns the first element that matches the predicate without creating an intermediate sequence.

✅ Use First(predicate) for better performance and cleaner code.

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

Select.where vs where.select

A

Select(…).Where(…): First projects all elements, then filters the projected results. less efficient if projection unnecessarily done on filtered elements

Where(…).Select(…): First filters elements, then projects only the filtered ones. Usually more efficient.

✅ Best practice: Filter first (Where), then project (Select).

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

Multiple enumeration and redundant chaining

A

Multiple Enumeration: Occurs when a deferred query is enumerated more than once, causing the query to run repeatedly and potentially impacting performance.

Redundant Chaining: Happens when LINQ methods are chained unnecessarily, creating extra intermediate sequences or steps that do not add value, slowing execution.

✅ Avoid both by materializing results (ToList()) when needed and optimizing method order.

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

What is enumeration

A

Enumeration in LINQ means iterating over a collection to access its elements, usually with foreach or methods that force query execution.

Deferred queries run only during enumeration.

Enumeration triggers execution of the query pipeline.

Examples: foreach, ToList(), Count(), First().

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

Problem with selectmany(inner=>inner).where(predicate)

A

Problem: Using SelectMany(inner => inner).Where(predicate) can enumerate all inner sequences first, then filter, which may be less efficient for large datasets.

✅ Better: Apply the filter inside SelectMany if possible:

SelectMany(inner => inner.Where(predicate))

This filters while flattening, avoiding unnecessary processing and improving performance.

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

DefaultIfEmpty().Select(…)

A

Problem: Using DefaultIfEmpty().Select(…) first creates a sequence with a default element if empty, then applies Select to all elements. This can unintentionally include the default value in projections and unnecessary

✅ Better: Apply Select before DefaultIfEmpty(). Apply fallback only if result empty

sequence.Select(x => x.Property).DefaultIfEmpty(defaultValue)

This ensures only the projected values are considered, avoiding surprises from the default element.

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

Single exceptions
Single or default

A

Empty sequence
No match or more than one match

Single or default exception when more than one match
When no match, default

17
Q

First vs FirstOrDefault

A

Empty sequence, no match exception

Default no exceptions

18
Q

Aggregate exceptions

A

Empty sequence with no seed

Aggregate() throws InvalidOperationException if the source sequence is empty and no seed value is provided.

If you provide a seed, Aggregate() never throws for an empty sequence; it returns the seed.

19
Q

Aggregate vs sum

A

Sum(): Built-in, optimized for numeric types. Returns the sum directly. Cannot perform custom operations.

Aggregate(): General-purpose. Can sum, multiply, or do any custom accumulation. Less optimized, may be slower for simple sums.

✅ Use Sum() for summing numbers, Aggregate() for custom or complex accumulation.

20
Q

Sum exceptions

A

Empty sequence value type always gives default

For nullable types (int?, double?), Sum() does not throw on an empty sequence. It returns null.

InvalidOperationException occurs only with Sum() on non-nullable types if using certain LINQ methods like Average() on an empty sequence, not Sum().

✅ So Sum() on empty nullable sequence → returns null, never throws.

21
Q

Where()

22
Q

First()

A

First element

23
Q

Single()

A

If sequence only 1 element then gives that. If no or more than 1. Exceptions

24
Q

First vs FirstOrDefault

A

First() – Use when you expect at least one element and not finding it is a logic error. Example:
Fetching a specific patient record by ID from a database that must exist.

FirstOrDefault() – Use when the element may not exist and you want a safe default instead of an exception. Example:
Searching for an optional discount for a customer; if none, return null or 0

Using First() throws an exception to catch logic errors early. It signals that your assumption “there is at least one element” is broken, so you can fix the bug immediately.

Using FirstOrDefault() + if check silently hides missing data, which may lead to subtle errors later.

✅ Rule: Use First() when empty sequence is invalid, FirstOrDefault() when empty is acceptable.

25
Todictionary exceptions
Always ensure the sequence is not null and keys are unique before calling ToDictionary(). Keys should not be null ArgumentNullException – if the source sequence or key selector is null. ArgumentException – if duplicate keys are generated by the key selector.
26
Take and Skip exceptions
No exceptions. Just gives few elements if count < n If n<0 exception
27
Then by exception
Needs orderby before
28
Orderly.orderby
Second predicate taken . First predicate overwritten