开发者

Puzzling Problem - C# Lambda Delegates disappearing after

I can't post a full working example right now, but I was hoping someone would have an idea of what might be going on here. (I'll try to toss together a small wo开发者_高级运维rking sample later this evening if no one can explain what might be happening from what's posted)

List<CVENT.Idea> ideas = ideaDAL.GetList(filter);

foreach (CVENT.Idea idea in ideas)  // Setup foreign key mapping
    BuildRelationships(idea, 8);

// Breakpoint set on next line.
ideas = (from   idea in ideaDAL.GetList(filter)
        where   IdeaSatisfiesCriteria(idea,filter)
        select  idea).ToList();
// I then Run To Cursor to This Line so I get a before and after the previous line.
foreach (CVENT.Idea idea in ideas)  // Setup foreign key mapping
    BuildRelationships(idea, 8);

return  ideas;

So I am loading some ideas from our DAL layer. This works fine. I then have a "BuildRelationships" function that assigns some Lambda expressions to Func delegate variables for each idea.

In Build Relationships function

private CVENT.Idea BuildRelationships(CVENT.Idea idea, int userID)
{
    idea.MapComments = thisIdea => commentBLL   .GetList(thisIdea.IdeaID, userID).ToList();
    return idea;
}

In my idea entity

public Func<Idea, List<Comment>> MapComments { get; set; }

This is a read only implementation of a Foreign Key Mapping Pattern where I am injecting the initialization for the foreign keys into my entity so that it can lazy load the foreign entity on demand.

The problem is that after the line I have the first breakpoint set on all of the Mapping variables are cleared to null (hence the second call to remap the relationships). I am guessing it has something with the creation of a new list because of ToList(), but what I don't understand is why the Mapping delegate variables aren't getting carried over with the rest of the properties. Any ideas?

(IdeaSatisfiesCriteria only does comparisons nothing is getting changed within the function.)


ideas = (from   idea in ideaDAL.GetList(filter)
        where   IdeaSatisfiesCriteria(idea,filter)
        select  idea).ToList();

All the mappings disappear because you are re-querying your ideas from the DAL instead of taking the existing list to which you applied the mappings. You probably intended to do this:

ideas = (from   idea in ideas 
        where   IdeaSatisfiesCriteria(idea,filter)
        select  idea).ToList();


You haven't really shown enough code to make it clear what's going on, but when you reassign ideas here:

ideas = (from   idea in ideaDAL.GetList(filter)
        where   IdeaSatisfiesCriteria(idea,filter)
        select  idea).ToList();

That isn't using the previous objects referred to within ideas at all as far as I can see... it's creating completely new objects, so why would you expect it to carry over any other properties? Presumably the other properties are being populated from a database - whereas you don't have anything in the database for the mappings, which is why you have to call BuildRelationships in the first place.

Basically, unless your DAL is meant to do some caching of the objects it's created, you're creating two lists of entirely different objects - so anything which isn't populated by whatever's creating the objects in the first place isn't going to be set.

Just to be clear, this has nothing to do with it being a lambda expression or a delegate - if you had any other sort of property which wasn't stored in the database, that would be "lost" too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜