tags:

views:

65

answers:

1

How can i check if a child-object of a linq-object is populated or not? Example code below.

My model have two methods, one joins data, and the other does not:

public static Member GetMemberWithPhoto(Guid memberId)
{
    using (DataContext db = new DataContext())
    {
        DataLoadOptions dataLoadOptions = new DataLoadOptions();
        dataLoadOptions.LoadWith<Member>(x => 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();
    }
}

Then my control have the following code:

Member member1 = Member.GetMemberWithPhoto(memberId);
Member member2 = Member.GetMember(memberId);

Debug.WriteLine(member1.UserPhoto.ToString());
Debug.WriteLine(member2.UserPhoto.ToString());

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.

What i would like is something like:

Debug.WriteLine((member1.UserPhoto.IsPopulated()) ? member1.UserPhoto.ToString() : "none");
Debug.WriteLine((member2.UserPhoto.IsPopulated()) ? member2.UserPhoto.ToString() : "none");

Is it possible?

A: 

Is the Settings property defined as an EntitySet? If so, then you should be able to inspect the HasLoadedOrAssignedValues property:

Debug.WriteLine(member1.Settings.HasLoadedOrAssignedValues ? member2.Settings.Count : -1);
luksan
You are of course correct. I just realized however that the code i have problem with is not an entityset, but a one-to-one relation in the database. I have updated the question accordingly.
TheQ
What does the property accessor look like for the one-to-one? Does it access a field of type EntityRef<T>? If so, you could create another property IsUserPhotoPopulated (perhaps on a partial class), that does a check on the field as follows _userPhoto.HasLoadedOrAssignedValue. Or, modify the logic of the getter to perform this check itself, although it will get overwritten when you regenerate your entity model (if it was auto-generated).
luksan
Both the Member and UserPhoto objects are autogenerared from corresponding tables via a dbml-file in visual studio, and as far as i can tell there are no EntityRef<T>'s anywhere. I could create new properties in partial classes, but i don't know if that will help. I could modify the logic of the queries as well, but it's a big project with lot's of old code, some joins the data and some does not, so i'd prefer if another method exists.
TheQ
What does the property acessor look like for UserPhoto if you view it in designer.cs? It should be accessing a field of EntityRef<T>, unless your Linq to SQL generates code differently from mine.
luksan
That did it, thanx! I created a new property in the partial class for Member called HasUserPhoto with "return this._UserPhoto.HasLoadedOrAssignedValue;".
TheQ