LINQ to
SQL provides a LoadWith operator that allows us to load the related table's data also.
The following query expression fetches the records from the Categories table, as
well as the records from the related table Items that matches with categoryID.
using (Deserts DesertsContext = new Deserts("Persist Security
Info=False;Initial Catalog=Deserts;Integrated
Security=SSPI;server=(local)"))
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith
(c => c.Category);
options.LoadWith(c => c.Name);
DesertsContext.LoadOptions = options;
Categories cat = DesertsContext.Categories.Single
(c => c.CategoryID == 1);
}
In the previous example, we used DataLoadOptions which defines the DataContext DataContext
load options. It loads all the tables that have a relationship with the main table.
Here, Categories entity class has an association with the Items entity. So whenever
the Categories entity gets loaded, the Items entity will also get loaded for the
corresponding category.
Pages:
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179