关于linq select和ToList()的问题

时间:2010-10-30 07:52:16

标签: c# linq

我通过执行Select LINQ查询返回列表时遇到问题。这是查询:

 var data = Repository<EducationString>
              .Find()
              .ToList() 
              .Select(p => new EducationStringModel() {
                      Id = p.Id,
                      Title = p.Title,
                      EducationDegree=p.EducationDegree })
              .ToList();

正如您所看到的,我使用ToList() 2次。我不知道为什么,但是当我删除第一个ToList()时,我看到了这个错误,“索引超出了数组的范围”,但同时有两个ToList()没问题。

如果我说EducationDegreeEducationStringModelIList<EducationDegree>会不会有帮助?

有谁知道原因?

@Mark:它的L2O

如果您需要查看课程:

公共类EducationStringModel {  private IList _educationDegree = new List(); 公共IList EducationDegree     {         得到         {             if(_educationDegree == null)             {                 _educationDegree = new List();             }             return _educationDegree;         }         设置{_educationDegree = value; }

}

public int? Id { get; set; }
public string Title { get; set; }

}

public class EducationString {

private string _title;     私人IList _educationExperiences;     私人IList _educationDegree;

virtual public string Title
{
    get { return _title; }
    set { _title = value; }
}

virtual public IList<EducationExperience> EducationExperiences
{
    get
    {
        if (_educationExperiences == null)
        {
            _educationExperiences = new List<EducationExperience>();
        }

        return _educationExperiences;
    }

    set
    {
        _educationExperiences = value;
    }

}

virtual public IList<EducationDegree> EducationDegree
{
    get
    {
        if (_educationDegree == null)
        {
            _educationDegree = new List<EducationDegree>();
        }
        return _educationDegree;
    }

    set
    {
        _educationDegree = value;
    }
}

}

1 个答案:

答案 0 :(得分:3)

这是实际的代码吗?唯一不明确的事情是:Find()返回什么?

它的声音就像ToList在这里通过破坏组合和使用LINQ-to-Objects来帮助,在这种情况下,AsEnumerable()应该也能正常工作。之后你只需做一个Select(对于L2O,它只是轮流拍摄每个项目并应用地图)。如果Find()是更具异国情调的东西,它听起来就像LINQ提供者中的一个错误(或者更公平地说:那个提供者正在努力应对非典型构造)。没有完全可重复的例子,很难说更多。