Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if a child-object is populated
    primarykey
    data
    text
    <p>How can i check if a child-object of a linq-object is populated or not? Example code below.</p> <p>My model have two methods, one joins data, and the other does not:</p> <pre><code>public static Member GetMemberWithPhoto(Guid memberId) { using (DataContext db = new DataContext()) { DataLoadOptions dataLoadOptions = new DataLoadOptions(); dataLoadOptions.LoadWith&lt;Member&gt;(x =&gt; x.UserPhoto); db.LoadOptions = dataLoadOptions; var query = from x in db.Members where x.MemberId == memberId select x; return query.FirstOrDefault(); } } public static Member GetMember(Guid memberId) { using (DataContext db = new DataContext()) { var query = from x in db.Members where x.MemberId == memberId select x; return query.FirstOrDefault(); } } </code></pre> <p>Then my control have the following code:</p> <pre><code>Member member1 = Member.GetMemberWithPhoto(memberId); Member member2 = Member.GetMember(memberId); Debug.WriteLine(member1.UserPhoto.ToString()); Debug.WriteLine(member2.UserPhoto.ToString()); </code></pre> <p>The last line will generate a "Cannot access a disposed object" exception. I know that i can get rid of that exception just by not disposing the datacontext, but then the last line will generate a new query to the database, and i don't want that. </p> <p>What i would like is something like:</p> <pre><code>Debug.WriteLine((member1.UserPhoto.IsPopulated()) ? member1.UserPhoto.ToString() : "none"); Debug.WriteLine((member2.UserPhoto.IsPopulated()) ? member2.UserPhoto.ToString() : "none"); </code></pre> <p>Is it possible?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload