5

At first,I do not use dynamic,I just use the code like this,and it works well.

  List<Student> result2 = StudentRepository.GetStudent(sex,age).ToList();
  IQueryable rows2 = result2.AsQueryable();

But when I change it to dynamic,it is wrong.

 dynamic result = GetPeopleData(sex,age);
 IQueryable rows = result.AsQueryable();

and I add a method like this,I build the project it show that List do not have the AsQueryable method.How to change it?

 private dynamic GetPeopleData(int sex, int age)
    {
        if(sex>30)
            return StudentRepository.GetStudent(sex,age).ToList();
        else
            return TeacherRepository.GetTeacher(sex, age).ToList();
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
flower
  • 2,212
  • 3
  • 29
  • 44
  • 6
    Why would you want to make it dynamic? – Jeroen Vannevel Mar 06 '15 at 03:51
  • You are showing the `GetPeopleData` method, but you call the `GetStudentData`. Is it right? – aush Mar 06 '15 at 03:57
  • Because I just want to get the data,not matter it come from student or teacher,I just want to change them to IQueryable rows and them export these rows. – flower Mar 06 '15 at 04:02
  • http://stackoverflow.com/questions/5311465/extension-method-and-dynamic-object-in-c-sharp ? – Alexei Levenkov Mar 06 '15 at 04:02
  • Go read up on inheritance and polymorphism. There is no reason why you should need to use `dynamic` here. You should almost never need to use `dynamic`. Also, what is the point of creating an `IQueryable` from a `List`? Calling `ToList()` on your repository eagerly fetches all records and loads them into memory; you lose any opportunity for query operators to execute on the server. – Mike Strobel Mar 09 '15 at 20:00

2 Answers2

5

AsQueryable() is an extension method and those don't work on dynamic.

Depending on what you want to do, there are several possible solutions:

  1. Don't use dynamic. Instead, make Student and Teacher implement a common interface (say, IPerson) and use that:

    private IReadOnlyList<IPerson> GetPeopleData(int sex, int age)
    {
        if (sex > 30)
            return StudentRepository.GetStudent(sex, age).ToList();
        else
            return TeacherRepository.GetTeacher(sex, age).ToList();
    }
    
    …
    
    var result = GetPeopleData(sex, age);
    IQueryable<IPerson> rows = result2.AsQueryable();
    
  2. Call AsQueryable() as a normal static method:

    dynamic result = GetPeopleData(sex, age);
    IQueryable rows = Queryable.AsQueryable(result);
    

BTW, checking whether sex is over 30 doesn't make any kind of sense to me. You should probably rethink that part of your design.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514
  • That possibly depends on what `sex` means here. I would rather not ask for more details. – Wiktor Zychla Mar 06 '15 at 13:45
  • @WiktorZychla The only alternative I can think of certainly couldn't be used to reliably distinguish between students and teachers. :-) – svick Mar 06 '15 at 13:56
-2

It is similar to this question.

As @jbtule said,Anonymous types are internal, if you cross assembly boundaries dynamic can't resolve the property.

Rather than using an anonymous type, try using an actual type or an Expando Object.

Community
  • 1
  • 1
Lyly
  • 718
  • 1
  • 8
  • 17
  • 1
    I don't believe this applies as OP did not show any usage of anonymous types... (assuming `GetTeacher` returns `List` similar to the other one). – Alexei Levenkov Mar 06 '15 at 04:45