📒
Notes
Cloud ComputingData Science/AIGame Development
  • Home
  • Big O
  • Data Structures & Algorithms
    • Data Structures
      • Array
      • Stack
      • Queue
      • Linked List
      • Binary Tree
    • Algorithms
      • Searching
      • Sorting
      • Graphs
        • Searching
        • Minimum Spanning Tree
        • Shortest Path Algorithms
      • String Algorithms
  • Object Oriented Programming
  • Languages
    • HTML/CSS
      • CSS
    • C++
    • C#
      • Types
      • Keywords
        • Modifiers
          • Access Modifiers
        • Method Parameters
      • Operators and Expressions
      • Collections
      • Constructors
      • Delegates
      • Indexers
      • Concepts
      • Features
        • LINQ
          • Operators
          • Working with Data
          • Methods
          • Resources
        • Asynchronous Programming
        • Reflection
    • Dart
    • GraphQL
    • JavaScript
      • Variable and Parameter
      • Built-in objects
        • Array
        • Built-in Functions
      • Functions
      • Classes
      • Prototype
      • Libraries
        • jQuery
        • React
          • Components
          • State and Lifecycle
          • Hooks
            • useState
            • useEffect
          • Resources
      • Testing Framework
      • Web APIs
    • Kotlin
      • Basics
    • Python
      • Basics
      • Data Structures
      • Functions
      • Resources
        • Flask
    • SQL
      • Basics
      • Operators
      • JOINs
      • Aggregations
      • Subqueries
      • Views
      • Functions
        • Window Functions
      • Stored Procedures
      • Performance Tuning
      • Extras
    • Resources
  • 🌐Web Frameworks
    • Angular
      • Templates
      • Directives
        • Attribute Directives
        • Structural Directives
    • ASP.NET
      • Fundamentals
        • Dependency Injection
        • Middleware
        • Session & State Management
      • Web apps
        • MVC
          • Controllers
            • Filters
          • Models
            • Model Binding
            • Model Validation
          • Views
            • Tag Helpers
            • View Components
          • Features
        • Client-side development
      • Web APIs
        • Controller-based APIs
        • Minimal APIs
        • OpenAPI
        • Content Negotiation
      • SignalR
      • Host and Deploy
        • IIS
      • Security
    • Django
      • The Request/Response Cycle
    • Terminologies
      • Web Server
        • Internet Information Services
    • Resources
  • 📱App Frameworks
    • Introduction
      • Resources
    • Xamarin
      • Lifecycle
      • Custom Renderers & Effects
      • Behaviors
      • Triggers
      • Gestures
      • Commands
      • Dependency Service in XF
      • Libraries
      • Showcase
    • .NET MAUI
      • Controls
      • Navigation
      • Storage Options
  • Multi-Platform Frameworks
    • .NET
      • .NET Framework
        • ADO.NET
        • WCF
      • Fundamentals
        • Logging
        • Testing
      • Advanced
        • Asynchronous Programming
        • Parallel Programming
        • Threading
        • Memory Management
          • Garbage Collection
    • Flutter
  • Object-Relational Mappers
    • Entity Framework
      • Application Models
      • Configuration
      • Setting Up
      • Advanced
  • Databases
    • Introduction
      • DBMS Architecture
      • Normalization
      • Database Transaction Models
    • Relational Databases
      • Microsoft SQL Server
        • Basics
        • Functions
        • Stored Procedures
        • Error Handling
        • Log Shipping
        • Querying and Manipulating JSON data
        • Statements
        • Topics
        • Extras
    • Non-Relational Databases
      • MongoDB
      • Redis
        • Data Structures
        • Introduction
        • Managing Database
  • Tools
    • Version Control
      • Git
        • Setup and Config
        • Basics
          • Sharing and Updating Projects
        • Resources
      • Perforce Helix
    • GitHub
    • Powershell
  • Software Development
    • Software Development Life Cycle
    • Software Design Patterns
      • GoF Design Patterns
      • Architectural Patterns
        • MVC
        • MVVM
        • N-tier Architecture
        • Onion Architecture
        • Data Transfer Objects
      • CQRS
    • Software Design Principles
      • S.O.L.I.D. Priniciple
  • System Design
    • Topics
      • Load Balancing
  • Topics
    • JWT
    • Caching
      • Static vs Dynamic Caching
    • OSI model
      • HTTP
    • Glossary
    • API
      • SOAP
      • REST
    • Microservices
    • WebHooks
    • Practice
    • Operating Systems
      • Windows
    • Architecture
  • 🔖Bookmarks
  • 🔗Resources
Powered by GitBook
On this page
  • Sorting
  • Filtering
  • Partitioning
  • Grouping

Was this helpful?

  1. Languages
  2. C#
  3. Features
  4. LINQ

Working with Data

Sorting

Method Name
Description
C# Query Expression Syntax

OrderBy

Sorts values in ascending order.

orderby

OrderByDescending

Sorts values in descending order.

orderby … descending

ThenBy

Performs a secondary sort in ascending order.

orderby …, …

ThenByDescending

Performs a secondary sort in descending order.

orderby …, … descending

Reverse

Reverses the order of the elements in a collection.

NA

string[] words = {"the", "quick", "brown", "fox", "jumps"};

IEnumerable<string> queryStx =
	from word in words
	orderby word.Length
	select word;

IEnumerable<string> methodStx = words.OrderBy(w => w.Length);

foreach (string str in queryStx)
	Console.Write(str + " ");

/* Output : the fox quick brown jumps */

Filtering

Method Name
Description
C# Query Expression Syntax

OfType

Selects values, depending on their ability to be cast to a specified type.

NA

Where

Selects values that are based on a predicate function.

where

string[] words = {"the", "quick", "brown", "fox", "jumps"};

IEnumerable<string> queryStx =
	from word in words
	where word.Length == 3
	select word;

IEnumerable<string> methodStx = words.Where(w => w.Length == 3);

foreach (string str in methodStx)
	Console.Write(str + " "); 

/* Output: the fox */

Partitioning

Method names
Description

Skip

Skips elements up to a specified position in a sequence.

SkipWhile

Skips elements based on a predicate function until an element does not satisfy the condition.

Take

Takes elements up to a specified position in a sequence.

TakeWhile

Takes elements based on a predicate function until an element does not satisfy the condition.

Chunk

Splits the elements of a sequence into chunks of a specified maximum size.

int[] numbers = {5, 9, 7, 2, 4, 3, 6, 1, 8};
foreach (var num in numbers.Take(3)) {
	Console.Write(num + "\t");
} /* Output : 5    9    7 */
foreach (var num in numbers.Skip(3)) {
	Console.Write(num + "\t");
} /* Output : 2    4    3    6    1    8 */
int chunkNumber = 1;
foreach (int[] chunk in Enumerable.Range(0, 9).Chunk(3)) {
	Console.Write($"Chunk {chunkNumber++}:");
	foreach (int item in chunk) {
		Console.Write($"    {item}");
	}

	Console.WriteLine();
}
// Output:
// Chunk 1:    0    1    2
// Chunk 2:    3    4    5
// Chunk 3:    6    7    8

Grouping

Method Name
Description
C# Query Expression Syntax

GroupBy

group … by -or- group … by … into …

ToLookup

NA

List<int> numbers = new List<int>(){35, 44, 200, 84, 3987, 4, 199, 329, 446, 208};
IEnumerable<IGrouping<int, int>> query =
	from number in numbers
	group number by number % 2;
	
foreach (var group in query) {
	Console.Write(group.Key == 0 ? "\nEven numbers: " : "\nOdd numbers: ");
	foreach (int i in group)
		Console.Write(i + " ");
}
PreviousOperatorsNextMethods

Last updated 2 years ago

Was this helpful?

Groups elements that share a common attribute. Each group is represented by an object.

Inserts elements into a (a one-to-many dictionary) based on a key selector function.

IGrouping<TKey,TElement>
Lookup<TKey,TElement>