2011-05-16 49 views
0

希望能夠做到下面這樣的事情。但是我有一個問題,因爲GetStudentByName方法不知道什麼類型的數據。我可以寫「data = data.Where(s => s.name ==」Some name「);」沒有任何問題。但是,我如何將它分成不同的方法?將LINQ分爲實體查詢以不同的方法

private IQueryable GetStudents() 
    { 
     var data = from a in db.Anvandning 
        select a; 
     return data; 
    } 
    public IQueryable GetStudentByName(string name) 
    { 
     var data = GetStudents(); 

     data = data.Where(s => s.name == name);  <-- Error occurs 
     return data; 
    } 

回答

1

您需要使用IQueryable<T>代替IQueryable,e.g

private IQueryable<Student> GetStudents() 
{ 
    var data = from a in db.Anvandning 
       select a; 
    return data; 
} 

public IQueryable<Student> GetStudentByName(string name) 
{ 
    var data = GetStudents(); 

    data = data.Where(s => s.name == name); 
    return data; 
} 

注意,這些方法可以更簡單地寫:

private IQueryable<Student> GetStudents() 
{ 
    return db.Anvandning.Select(x => x); 
} 

public IQueryable<Student> GetStudentByName(string name) 
{ 
    return GetStudents().Where(s => s.name == name); 
}