選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1.5KB

Skill 11 - LINQ Querying

Core Concepts

LINQ lets code describe what sequence result is wanted: filter, project, sort, group, join, aggregate, and transform.

LINQ Parts

  • Extension methods such as Where, Select, OrderBy, GroupBy, and Join provide the operations.
  • Providers execute expressions against in-memory objects, EF Core, XML, or other sources.
  • Lambda expressions express predicates and projections.
  • Query comprehension syntax is optional and maps to method calls.

Rules

  • Prefer method syntax for simple pipelines.
  • Consider query syntax for multi-join/group queries when readability improves.
  • Understand deferred execution: queries may not run until enumerated.
  • Materialize with ToList, ToArray, or similar only when needed.
  • Avoid multiple enumeration of expensive queries.
  • Place filters early.
  • For EF Core, ensure the query can translate to SQL.
  • Do not call custom .NET methods inside EF queries unless they can be translated.

Common Operations

  • Where filters.
  • Select projects.
  • OrderBy / ThenBy sorts.
  • GroupBy groups.
  • Join, GroupJoin, LeftJoin, and RightJoin combine sequences where available.
  • Any checks existence.
  • All checks universal conditions.
  • Count, Sum, Average, Min, Max aggregate.

Performance Rules

  • Avoid materializing before filtering.
  • Use Any() instead of Count() > 0 for existence.
  • Use TryGetNonEnumeratedCount when count may be available cheaply.
  • Be careful with closures in loops.

Powered by TurnKey Linux.