[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Force the load collection linq

Posted on 2009-05-01
3
Medium Priority
?
551 Views
Last Modified: 2013-11-11
Hi!

I've 2 tables

by exemple:

Categories and Models

One categories has many Models
One models has only one categories(so we've a foreign key in models)

So I use LINQ to Entity, but I've a problem.

If I've a category object and I do:

category.Models.count();

I've "0", and in the database, I've correct things.

And if I debug when I do this, a property named "isLoaded" that have the value "false".

So I think the collection isn't loaded.

Must we do anything special? How do we load this collection?

Thanks!
0
Comment
Question by:Nargzul
3 Comments
 
LVL 4

Expert Comment

by:CoveyCraig
ID: 24277647
Read this article on loading categories:  http://blogs.msdn.com/meek/archive/2008/12/07/stored-procedures-and-first-class-relationships-in-the-entity-framework.aspx

Load using this syntax:  Category models = context.Categories.Where(c => c.Name == "Model").First();

0
 
LVL 21

Expert Comment

by:naspinski
ID: 24277680
You might want to do some eager loading - I explain it here: http://naspinski.net/post/You-might-be-making-a-lot-of-unneccessary-trips-to-your-DB-with-Linq-to-SQL.aspx
dbDataContext db = new dbDataContext();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<category>(i => i.Model);
db.LoadOptions = dlo; 

Open in new window

0
 
LVL 1

Accepted Solution

by:
Nargzul earned 0 total points
ID: 24361707
I've used the ".Include("blabla")".

It's exactly what I need
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question