Logo
Logo
  • Blazor CMS Home
  • Oqtane Tutorials
  • Blazor CMS Home
  • Oqtane Tutorials
Oqtane Tutorials
Oqtane Tutorials

LINQ Tutorial (Language INtegrated Query)

Tutorial Home › LINQ
Use DNN APIs to get DNN Data
#1 LINQ Basics
LINQ Relationships - Children, Parents, Grandchildren, ...
Resources
  • LINQ Manual
  • Working with LINQ and 2sxc/EAV Data
  • Querying Data and Data Sources with code and LINQ

LINQ Basics

Learn how to leverage LINQ (Language Integrated Query) of C# to sort, filter, group content-items. This demo uses the following data in app:

  • Persons - various people who are used in the data. A person can also have one or many favorite books.
  • Books - books people wrote or contributed to. Books have authors and

Some notes before we start

All our code uses some general stuff explained here:

  • to enable LINQ commands we always need:
    @using System.Linq
  • most of the code starts by retrieving a list of Books and Authors. This is done using:
    App.Data["Books"]
  • Since we want to use dynamic types (which lets us write things like book.Name, we usually wrap it with:
    AsList(App.Data["Books"])
  • The compiler often can't guess object types we are using, we often need to cast lists to:
    IEnumerable<dynamic>
    The easiest way is to just run it through AsList(original as object).
    The as object part necessary because of limitations in Razor.
The samples can differ based on your Razor base class or if you're running an old version.
Switch to Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)

Simple Where(...) and Any()

Simple First() and Last()

This filters the authors with long first names.

Take() / Skip()

Count() and Count

Count some stuff.

Simple Sorting of Persons

 

 

Use DNN APIs to get DNN Data
#1 LINQ Basics
LINQ Relationships - Children, Parents, Grandchildren, ...
  • 2sic internet solutions
  • Langäulistrasse 62, 9470 Buchs SG, Switzerland
  • +41 81 750 67 77
Login