jero
jero

Reputation: 543

WCF, Linq Error:cannot implicitly convert type System.linq.iorderedQueryable<> to System.Collection.Generic.List<>

I am getting an error : i am using entity framework, wcf.

Error:cannot implicitly convert type System.linq.iorderedQueryable<xDataModel.Info> to System.Collection.Generic.List<xServiceLibrary.Info>

Below are my code:


WCF Service:

namespace xServiceLibrary
{
    public List<Info> GetScenario()
            {
                xEntities db = new xEntities();
                 var query = from qinfo in db.Infoes
                                select qinfo;

                  //return query.Cast<Info>().ToList(); (not working)
                  //return query.toList(); (not working)
                    return query;
       }
}

Interface:

namespace xServiceLibrary
{
     [OperationContract]
            List<Info> GetScenario();
}

Class:

namespace xServiceLibrary
{
       [DataContract]
        public class Info
        {
            [DataMember]
            public int Scenario_Id;

            [DataMember]
            public string Scenario_Name { get; set; } 

            [DataMember]
            public string Company_Name { get; set; } 
        }
}

update:(2) I have two class library files. One is xDataModel namespace in which i have created xmodel.edmx file. second is xServiceLibrary namespace where i am implementing Wcf Service. i have attached the xDataModel.dll file in my xServiceLibrary so that i could query my EF Model.

i am not able to understand the concept. any help would be appreciated.

Upvotes: 0

Views: 771

Answers (2)

Michael Edenfield
Michael Edenfield

Reputation: 28338

The problem is that you have two different classes, both called Info, both in scope at the time you run your query. This is a very very bad thing, especially if you thought they were the same class.

If DataModel.Info and ServiceLibrary.Info are the same class, you need to figure out why they are both in scope at the same time and fix that.

If they are different classes, you need to be explicit about which one you are trying to return. Assuming that your EF model includes a set of DataModel.Info objects, your options there are:

  1. Return a List<DataModel.Info> which you can get by calling query.ToList()
  2. Return a List<ServiceLibrary.Info> which you can get by copying the fields from your DataModel.Info objects:

    var query = from qinfo in db.Info
            select new ServiceLibrary.Info
            {
              Scenario_Id = q.Scenario_Id,
              Scenario_Name = q.Scenario_Name
              Company_Name = q.Company_Name
            };
    
  3. Return something else, such as your custom DTO object, similar to #2 but with only the specific fields you need (e.g. if ServiceLibrary.Info is a heavy object you don't want to pass around.

In general, though, your problem is centered around the fact that the compiler is interpreting List<Info> as List<ServiceLibrary.Info> and you probably don't want it to.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160912

The problem is that you have two different types named Info: DataModel.Info and ServiceLibrary.Info - because these are different types you cannot cast one into the other.

If there is no strong reason for both being there I would eliminate one of them. Otherwise as a workaround you could project DataModel.Info to ServiceLibrary.Info by copying the relevant properties one by one:

var results = (from qinfo in db.Infoes
               select new ServiceLibrary.Info()
               {
                    Scenario_Id = qinfo.Scenario_Id,
                    //and so on
               }).ToList();

Upvotes: 1

Related Questions