开发者

Populate a property in load options from a child object

I'm using Linq to sql to populate my object. Previously, I was using deferred loading and then iterating through my list accessing the child objects to force loading. This isn't a very good solution with the large data set I have so I am now setting the LoadOptions in my data context to grab it all initially.

The one problem I've run into is that before I was manually loading a single property on each iteration through my list and I'm not sure how to accomplish this now. It's just a simple string value.

 info.CreatedByName = info.CreatedBy.Name;

In my data context, I'm specifying my load options as such:

 DataLoadOptions loadOptions = new DataLoadOptions();
 loadOptions.LoadWith<Info>(info => info.Owner);
 loadOptions.LoadWith<Info>(info => info.CreatedBy);

Is there a way to specify assignment of this property in my load options? Something like:

 loadOpt开发者_如何学Goions.LoadWith<Info>(info => info.CreatedByName)


I don't think that you can use LoadOptions in this way. You could use a projection container class, if you don't need to perform object tracking, and explicitally set the property upon construction.

from info in context.Infos
select new InfoContainer
{
    CreatedByName = CreatedBy.Name
}

You could add a partial class for Info and add a property that refers to CreatedBy.Name

public string CreatedByName
{
    get
    {
        return CreatedBy.Name;
    }
}

That would assume that the CreatedBy is loaded, or lazy load it if its not populated.


Everything you do is perfectly valid, yet you must explicitly select CreatedBy in your query after setting context's LoadWith options, eg

var result = from info in context.Infos
             join user in context.Users on info.CreatedById equals user.Id
             select info;

I've had the same problem, check this solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜