首页 文章

关于linq select和ToList()的问题

提问于
浏览
3

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

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

如你所见,我使用了 ToList() 次 . 我不知道为什么,但是当我删除第一个 ToList() 时,我看到了这个错误,"Index was outside the bounds of the array",但是通过同时使用 ToList() 没有问题 .

如果我说 EducationStringModel 中的 EducationDegreeIList<EducationDegree> 会有帮助吗?

有谁知道原因?

@Mark:它的L2O

如果你需要看 class :

public class EducationStringModel {private IList _educationDegree = new List(); public IList EducationDegree {get {if(educationDegree == null){ editucationDegree = new List(); } return _educationDegree; } set

}

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 回答

  • 3

    那是实际的代码吗?唯一不清楚的事情是:Find()返回什么?

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

相关问题