Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Controller

C#
public ActionResult Stations_Read([DataSourceRequest]DataSourceRequest request)
  {
     IQueryable<Station> stations = db.Stations;
     DataSourceResult result = stations.ToDataSourceResult(request, station => new 
           {
              Id = station.Id,
              Code = station.Code,
              Name = station.Name,
              SubZone = new SubZone()
              {
                  Name = station.SubZone.Name // got error here.
              }
          });

          return Json(result);
      }


Class

C#
public class Station : Entity
   {
       public string Code { get; set; }
       public string Name { get; set; }
       public Guid SubZoneId { get; set; }
       public virtual SubZone SubZone { get; set; }
   }


What I have tried:

SubZone = new SubZone()
       {
          Name = station.SubZone.Name // got error here.
       }
Posted
Updated 8-Dec-16 0:48am
Comments
Er. Puneet Goel 8-Dec-16 4:05am    
What kind of error ?
wa.war 8-Dec-16 4:11am    
"An error occurred while executing the command definition. See the inner exception for details."
- I cant get the Code and Name from Subzone.
F-ES Sitecore 8-Dec-16 7:08am    
So what does the inner exception say?

1 solution

You can't create instance of new SubZone() [Entity Class] while projecting data from IQueryable collection. You have to convert it into ToList than you can use.
 
Share this answer
 
Comments
wa.war 8-Dec-16 21:09pm    
thanks for respond. can you show me how to do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900