2010-10-30 34 views
3

我有通过执行Select LINQ查询返回一个列表中的问题。这是查询:问题有关LINQ选择和ToList()

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>会有帮助吗?

是否有任何人谁知道什么原因呢?

@马克:其L2O

如果u需要看到的类:

公共类EducationStringModel { 私人IList的_educationDegree =新名单(); 公众的IList EducationDegree { 得到 { 如果(_educationDegree == NULL){ _educationDegree =新名单(); } return _educationDegree; } set {_educationDegree = value; }

} 

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

}

公共类EducationString {

私人字符串_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; 
    } 
} 

}

+3

什么是查找()返回? – 2010-10-30 08:03:58

+0

你find方法应该返回的IEnumerable或IList的... – 2010-10-30 08:14:53

回答

3

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

听起来像ToList在这里帮助打破构图和使用LINQ到对象,在这种情况下AsEnumerable()应该也一样。之后,你只需要做一个选择(对于L2O来说,每个项目轮流使用并应用地图)。如果查找()是更多的东西异国情调,它听起来像在LINQ提供程序中的错误(或者更公平的:即供应商疲于应对非典型结构)。如果没有完全可重复的例子,很难说更多。

+0

find()方法返回的IQueryable Adrakadabra 2010-10-30 08:17:23

+1

@adrakadabra - 不说了很多;什么是*提供者*? L2O? EF? L2S?道夫? – 2010-10-30 08:38:41

+0

@Mark:我编辑过我的帖子 – Adrakadabra 2010-10-30 11:58:47