> For the complete documentation index, see [llms.txt](https://csharp.progdocs.se/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://csharp.progdocs.se/annat/linq/linq-queries.md).

# Linq-queries

Linq queries är databasfrågor som fungerar likadant oavsett om man kör dem mot en databas eller vilken [samling ](/klasser-och-objektorientering/generiska-klasser.md#samlingar)som helst.

```csharp
List<Character> characters = new()
{
  new Character() {Name="Linda"},
  new Character() {Name="Micke"},
  new Character() {Name="Mira"},
  new Character() {Name="Pontus"},
  new Character() {Name="George"},
};

var shortNames =
            from c in characters
            where c.Name.Length < 6
            orderby c.Name
            select c;
            // Samlingen shortNames innehåller karaktärerna Micke, Mira och Linda,
            // sorterade i bokstavsordning efter namn.
```

Det finns en hel del man kan göra med Linq, men oftast klarar man sig med **from**, **in**, **where**, **orderby** och **select**.

## From…in

From och in anger tillsammans *varifrån* datan ska hämtas, och vilken variabel varje rad tillfälligt ska lagras i. Först skrivs from, därefter namnet på en variabel (som inte behlver skapas innan eller ger datatyp), därefter in och slutligen datakällan.

```csharp
var shortNames = from c in characters
            select c;
```

## Where

Where anger ett villkor som fungerar som ett filter – bara de föremål i samlingen (eller rader i tabellen) som matchar kriteriet får vara med i resultatet.

```csharp
var user = from c in characters
           where c.Name == "Micke" && c.Password == "12345"
           select c;
```

## Orderby

Orderby avgör hur resultaten *sorteras*.

```csharp
var shortNames =
            from c in characters
            where c.Name.Length < 6
            orderby c.Age
            select c;
            // Samlingen shortNames innehåller karaktärerna Micke, Mira och Linda,
            // sorterade efter ålder.
```

## Select

Select avgör *vad som läggs in* i resultatsamlingen.

```csharp
var shortNames =
            from c in characters
            where c.Name.Length < 6
            orderby c.Name
            select c.Name;
            // shortNames blir en string-samling med karaktärernas namn
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://csharp.progdocs.se/annat/linq/linq-queries.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
