Veltar
Veltar

Reputation: 741

Explanation needed about linq queries

private bedrijf_modelDataContext dc = new bedrijf_modelDataContext();

public IList<Afdeling> selectAll()
{ 
    var result = from a in dc.Afdelings
        select a;
    return result.ToList();
}

This code is supposed to return all the records from the Afdeling-table. This code works, but it comes from my teacher, and there is no explanation whatsoever as to how this works. Can somebody explain what this exactly does? Thank you.

Upvotes: 0

Views: 62

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

private bedrijf_modelDataContext dc = new bedrijf_modelDataContext();

Creates a DataContext. Think of it as a workspace + database connection. It tracks the loaded entities.

var result = from a in dc.Afdelings
    select a;

Is a Linq query that retreives the records as objects. In this case everything from the table. The query is not executed immediately, Linq has 'deferred execution'.

 return result.ToList();

The ToList() fetches all the records (counters the deferred execution).

Upvotes: 3

Related Questions