> ## Documentation Index
> Fetch the complete documentation index at: https://docs.s4kit.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Querying

> Filter, sort, and paginate your SAP data

## Query Options

The SDK supports all standard OData query options for filtering, sorting, and paginating data.

```typescript theme={null}
const results = await client.A_BusinessPartner.list({
  select: ['BusinessPartner', 'BusinessPartnerName'],
  filter: "BusinessPartnerCategory eq '1'",
  orderBy: 'BusinessPartnerName asc',
  top: 10,
  skip: 0,
  expand: ['to_BusinessPartnerAddress']
});
```

## Select Fields

Limit which fields are returned to reduce payload size:

```typescript theme={null}
// Select specific fields
const partners = await client.A_BusinessPartner.list({
  select: ['BusinessPartner', 'BusinessPartnerName', 'Industry']
});
```

## Filtering

Filter results using OData filter syntax.

### Basic Filters

```typescript theme={null}
// Equality
const customers = await client.A_BusinessPartner.list({
  filter: "BusinessPartnerCategory eq '1'"
});

// Not equal
const nonCustomers = await client.A_BusinessPartner.list({
  filter: "BusinessPartnerCategory ne '1'"
});

// Numeric comparison
const largeOrders = await client.A_SalesOrder.list({
  filter: "TotalNetAmount gt 10000"
});

// Date comparison
const recentOrders = await client.A_SalesOrder.list({
  filter: "CreationDate ge 2024-01-01"
});
```

### Filter Operators

| Operator | Description           | Example            |
| -------- | --------------------- | ------------------ |
| `eq`     | Equal                 | `"Status eq 'A'"`  |
| `ne`     | Not equal             | `"Status ne 'X'"`  |
| `gt`     | Greater than          | `"Amount gt 100"`  |
| `ge`     | Greater than or equal | `"Amount ge 100"`  |
| `lt`     | Less than             | `"Amount lt 1000"` |
| `le`     | Less than or equal    | `"Amount le 1000"` |

### String Functions

```typescript theme={null}
// Contains
const searchResults = await client.A_BusinessPartner.list({
  filter: "substringof('Corp', BusinessPartnerName)"
});

// Starts with
const aPartners = await client.A_BusinessPartner.list({
  filter: "startswith(BusinessPartnerName, 'A')"
});

// Ends with
const ltdCompanies = await client.A_BusinessPartner.list({
  filter: "endswith(BusinessPartnerName, 'Ltd')"
});
```

### Logical Operators

```typescript theme={null}
// AND
const activeCustomers = await client.A_BusinessPartner.list({
  filter: "BusinessPartnerCategory eq '1' and IsActiveEntity eq true"
});

// OR
const customersOrSuppliers = await client.A_BusinessPartner.list({
  filter: "BusinessPartnerCategory eq '1' or BusinessPartnerCategory eq '2'"
});

// NOT
const notDeleted = await client.A_BusinessPartner.list({
  filter: "not(IsMarkedForDeletion)"
});

// Complex combinations
const complexFilter = await client.A_BusinessPartner.list({
  filter: "(BusinessPartnerCategory eq '1' and Industry eq 'TECH') or TotalNetAmount gt 100000"
});
```

### Null Checks

```typescript theme={null}
// Check for null
const withoutIndustry = await client.A_BusinessPartner.list({
  filter: "Industry eq null"
});

// Check not null
const withIndustry = await client.A_BusinessPartner.list({
  filter: "Industry ne null"
});
```

## Sorting

Order results by one or more fields:

```typescript theme={null}
// Single field, ascending
const sorted = await client.A_BusinessPartner.list({
  orderBy: 'BusinessPartnerName'
});

// Single field, descending
const sortedDesc = await client.A_BusinessPartner.list({
  orderBy: 'BusinessPartnerName desc'
});

// Multiple fields
const multiSorted = await client.A_SalesOrder.list({
  orderBy: 'SoldToParty asc, CreationDate desc'
});
```

## Pagination

### Basic Pagination

```typescript theme={null}
const pageSize = 10;

// First page
const page1 = await client.A_BusinessPartner.list({
  top: pageSize,
  skip: 0
});

// Second page
const page2 = await client.A_BusinessPartner.list({
  top: pageSize,
  skip: pageSize
});
```

### With Total Count

```typescript theme={null}
const pageSize = 10;
const page = 1;

const { data, count } = await client.A_BusinessPartner.listWithCount({
  top: pageSize,
  skip: (page - 1) * pageSize
});

const totalPages = Math.ceil(count / pageSize);
console.log(`Page ${page} of ${totalPages}`);
```

### Async Iterator

For processing large datasets efficiently:

```typescript theme={null}
// Iterate over all pages
for await (const partners of client.A_BusinessPartner.paginate({ pageSize: 100 })) {
  for (const partner of partners) {
    console.log(partner.BusinessPartnerName);
  }
}
```

## Expanding Relations

Fetch related entities in a single request:

```typescript theme={null}
// Single expand
const partnersWithAddresses = await client.A_BusinessPartner.list({
  expand: ['to_BusinessPartnerAddress']
});

// Multiple expands
const ordersWithDetails = await client.A_SalesOrder.list({
  expand: ['to_Item', 'to_Partner']
});

// Nested expand (OData v4)
const deepExpand = await client.A_SalesOrder.list({
  expand: ['to_Item/to_Material']
});
```

### Expand with Select

```typescript theme={null}
const partners = await client.A_BusinessPartner.list({
  select: ['BusinessPartner', 'BusinessPartnerName'],
  expand: [
    {
      property: 'to_BusinessPartnerAddress',
      select: ['AddressID', 'CityName', 'Country']
    }
  ]
});
```

## Full-Text Search

Search across multiple fields (if supported by the service):

```typescript theme={null}
const searchResults = await client.A_BusinessPartner.list({
  search: 'Acme'
});
```

## Query Builder

For complex queries, use the fluent query builder:

```typescript theme={null}
const results = await client.A_BusinessPartner
  .query()
  .select('BusinessPartner', 'BusinessPartnerName', 'Industry')
  .filter('BusinessPartnerCategory', 'eq', '1')
  .filter('Industry', 'in', ['TECH', 'RETAIL'])
  .orderBy('BusinessPartnerName', 'asc')
  .top(10)
  .expand('to_BusinessPartnerAddress')
  .execute();
```

## Common Patterns

### Date Range Filter

```typescript theme={null}
const startDate = '2024-01-01';
const endDate = '2024-12-31';

const ordersInRange = await client.A_SalesOrder.list({
  filter: `CreationDate ge datetime'${startDate}' and CreationDate le datetime'${endDate}'`
});
```

### In List Filter

```typescript theme={null}
const specificPartners = await client.A_BusinessPartner.list({
  filter: "BusinessPartner eq '10100001' or BusinessPartner eq '10100002' or BusinessPartner eq '10100003'"
});
```

### Case-Insensitive Search

```typescript theme={null}
const results = await client.A_BusinessPartner.list({
  filter: "tolower(BusinessPartnerName) eq 'acme'"
});
```
