Return query in a method. RRS feed

  • Question

  • Hi, I have a query and I save it in a var but I can't return it. 

    I need return the query because I use a foreach in other method.

    Query + Method

    public object MiMethod(){

                        var join = (from c in db.Cars
                                join cp in db.CarsPersons on e.IdCar equals p.IdPerson
                                join p in db.Persons on cp.IdPerson equals p.IdPerson
                                select new { Car = c, CarsPersons = cp, Person = p }).ToList();

     return join


    The problem is the "object" return. I can't use it.

    Thanks for help me!

    Tuesday, November 21, 2017 3:19 AM

All replies

  • Define a special class for such data, for example:

    Class MyClass
       Car car;
       List<CarPerson> carPerson;
       Person person;

    Use the corresponding types.

    Then adjust the final part:

       select new MyClass { car = c, carPerson = cp.ToList(), person = p }).ToList();


    Instead of above changes, you can also try the dynamic type:

    public dynamic MiMethod() {    . . . }

    Then the caller can access the Car, CarPersons and Person members (without IntelliSense though).

    Tuesday, November 21, 2017 9:52 AM