WCF-Problem when implementing method with Entity Framework

0

I am developing an application in WCF. I have created a service and I need to implement a method that returns a list from a query to the Entity Framework by joining several tables with joins. I do not know if I'm doing well

This is the code:

 public List<Object> GetPhones()
 {
            var ge = new gesphoneEntities();
            var phones = from ph in ge.tbl_phones
                         join phm in ge.tbl_phone_models on    ph.tbl_phone_model_id equals phm.ID
                         join phf in ge.tbl_phone_fabricants on phm.tbl_phone_model_fabricant_id equals phf.ID
                         select new object() { id = ph.ID, model = phm.Name };

            return (List<Object>)phones;
 }

And the error that gives me is this.

    
asked by Aritz Ezkiaga 04.06.2017 в 19:02
source

3 answers

0

Hello, your problem is in the return type. Your variable phones is of a different type than a list. Execute the ToList() method to create a list of the query by executing it. Test

return phones.ToList();
    
answered by 04.06.2017 в 23:26
0

The truth is that I am very surprised that a List<Object> is returned, in my view you should return a typed list.

  public List<Phone> GetPhones()
  {
       var ge = new gesphoneEntities();
       var phones = from ph in ge.tbl_phones
             join phm in ge.tbl_phone_models on    ph.tbl_phone_model_id equals phm.ID
             join phf in ge.tbl_phone_fabricants on phm.tbl_phone_model_fabricant_id equals phf.ID
             select new Phone() { id = ph.ID, model = phm.Name };

       return phones.ToList();
  }
    
answered by 05.06.2017 в 10:04
0

Why do not you better use a LINQ query that is cleaner and less cumbersome. Another thing you are asking about Ids like phm.Id, phf.Id that apparently you should have passed me as parameters. I hope it serves you something

    
answered by 05.06.2017 в 13:49