0

At first, I have a variable in my controller to store linq query result as shown below:

var test= from m in db.testTable
          join n in db.testTable2
          on m.ID equals n.ID into tabA
          from a in tabA
          join o in db.testTable3
          on m.UserID equals o.ID
          select new { .................};
if (test.Count() > 0)
{
    foreach (var k in test)
    {
        ViewData["dropDown_xxx"] = test.Select(j => new { Value = j.ID, Text = j.Description})
                                   .AsEnumerable()
                                   .Select(j => new SelectListItem // Work with in-memory objects using LINQ to Objects instead of LINQ to Entities
                            {
                                Value = j.Value.ToString(),
                                Text = j.Text
                            }).ToList();
                    }  

The above works perfectly and I able to generate the dropdownlist and the ViewData.
Now I'm trying to move this linQ query into another class instead of putting in this controller as shown below:

public class testClass{
    public IQueryable<dynamic> TestMethod(){
        return from m in db.testTable
        join n in db.testTable2
        on m.ID equals n.ID into tabA
        from a in tabA
        join o in db.testTable3
        on m.UserID equals o.ID
        select new { .................};
    }
}     

And now calling it in my previous Controller:

       var test= new testClass().TestMethod();
if (test.Count() > 0)
{
    foreach (var k in test)
    {
        ViewData["dropDown_xxx"] = test.Select(j => new { Value = **j.ID**, Text = **j.Description**})
                                   .AsEnumerable()
                                   .Select(j => new SelectListItem // Work with in-memory objects using LINQ to Objects instead of LINQ to Entities
                            {
                                Value = j.Value.ToString(),
                                Text = j.Text
                            }).ToList();
                    }  

May I know what's wrong with the code? At J.ID and J.Description it's now complaining that An expression tree may not contain a dynamic operation

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117

1 Answers1

0

here your dynamic type only inside the scope of the TestMethod. You better create new class and return that class to the caller.

public class testClass{
    public IQueryable<MyClass> TestMethod(){
        return from m in db.testTable
        join n in db.testTable2
        on m.ID equals n.ID into tabA
        from a in tabA
        join o in db.testTable3
        on m.UserID equals o.ID
        select new MyClass{ .................};
    }
}    

If you need to return anonymous type then check this

Can't return anonymous type from method? Really?

Damith
  • 62,401
  • 13
  • 102
  • 153
  • Is there any other better way rather than creating a new class? I can let go of using the "dynamic" if there is workaround other than creating a new class – SuicideSheep Sep 04 '13 at 07:24
  • My bad for not specifying clearly the language been used. This is actually MVC and hence there isn't any CAST method available as shown in the link you shared. Thanks for your help. – SuicideSheep Sep 04 '13 at 07:33