为什么这个LINQ查询只新建一个内部List实例

本文关键字:一个 内部 List 实例 新建 LINQ 查询 为什么 | 更新日期: 2024-09-19 06:59:32

根据要求,我简化了这个问题。当尝试使用两个泛型List并将它们混合时,我得到了意想不到的结果。

private List<ConditionGroup> GetConditionGroupParents()
{    
    return (from Conditions in dataContext.Conditions
               orderby Conditions.Name
               select new ConditionGroup
               {
                   GroupID = Conditions.ID,
                   GroupName = Conditions.Name,
/* PROBLEM */      MemberConditions = new List<Condition>()
               }).ToList();
}
private List<ConditionGroup> BuildConditionGroups()
{
    var results = GetConditionGroupParents();
    // contents of ConditionMaps is irrelevant to this matter
    List<ConditionMap> ConditionMaps = GenerateGroupMappings();
    // now pair entries from the map into their appropriate group,
    // adding them to the proper List<MemberConditions> as appropriate
    foreach (var map in ConditionMaps)
    {
        results.Find(groupId => groupId.GroupID == map.GroupID)
               .MemberConditions.Add(new ConditionOrphan(map));
    }
    return results;
}

我希望ConditionMaps中的每个映射都映射到"results.Find…"语句中的单个ConditionGroup的MemberConditions。

相反,每个映射都被添加到每个组的列表中,并且同时发生。

[edit] I've since proven that there is only a single instance of 
       List<Memberconditions>, being referenced by each group.
       I unrolled the creation of the groups like so:
       .
       .
       .    
       /* PROBLEM */      MemberConditions = null }).ToList();
      foreach (var result in results)
      {
          List<Condition> memberConditions = new List<Condition>();
          results.MemberConditions = memberConditions;
      }
      return results;
      In that case I was able to watch each instantiation stepping 
      through the loop, and then it worked as expected.  My question 
      remains, though, why the original code only created a single 
      instance.  Thanks!
      .

为什么GetConditionGroupParents中的LINQ查询没有为每个组"新建"一个唯一的MemberConditions列表,如上面/*问题*/注释所示?

任何见解都值得赞赏。谢谢

Jeff Woods读取,PA

为什么这个LINQ查询只新建一个内部List实例

这是一个错误。作为一种变通方法,您可以创建一个工厂功能

static List<T> CreateList<T>(int dummy) { ... }

并根据当前行(如Conditions.ID)将任何伪值传递给它。

这个技巧之所以有效,是因为与EF不同,L2S能够在查询的最后一个Select中调用不可翻译的函数。迁移到EF不会很有趣,因为他们还没有实现这一点。