ChhavishJ
ChhavishJ

Reputation: 501

Accessing the value of query returning list in controller

Here is my schedule class

[Table("Schedules", Schema = "Expedia")]
public  class Schedule
    {

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ScheduleId { get; set; }

        [ForeignKey("Inventory")]
        public int FlightId { get; set; }
        public FlightInventory Inventory { get; set; }

        [Display(Name = "Departure city")]
        public string DepartureCity { get; set; }

        [Display(Name = "Arrival city")]
        public string DestiationCity { get; set; }

        [Display(Name = "Departure date")]
        [Required]
        public string DepartureDate { get; set; }

        [Required]
        [Display(Name = "Arrival date")]
        public string ArrivalDate { get; set; }

        [Required]
        [Display(Name = "Departure Time")]
        public string Departure { get; set; }

        [Required]
        [Display(Name = "Arrival Time")]
        public string Arrival { get; set; }

        [Display(Name = "Price/Ticket")]
        public int Price { get; set; }
    }
}

here is my context class

 public class UsersContext : DbContext
 {
     public UsersContext() 
         : base("Expedia") { }

     public DbSet<Schedule> schedules { get; set; }
 }

Here is My Function Returning the query result:

 public dynamic Details(int ScheduleID)
 {
     var query =(from f in db.schedules.Where(f => f.ScheduleId== ScheduleID)
                 select new
                 {
                     f.ScheduleId,
                     f.DepartureDate,
                     f.ArrivalDate,
                     f.Departure,
                     f.Arrival,
                     f.DepartureCity,
                     f.DestiationCity,
                     f.Inventory.provider,
                     f.Inventory.nonstop,
                     f.Price,
                     f.Inventory.Available
                 }).ToList();

     return query.AsEnumerable();
 }

When I call this in my controller:

var result= x.Details(selectedID);

the result variable shows null value although the query is returning a value.

How to access the query result in the controller?

Please provide any link, reference, hint.

Upvotes: 1

Views: 367

Answers (3)

mehmet mecek
mehmet mecek

Reputation: 2685

Return type of Details method must be IEnumerable< dynamic >.

public IEnumerable<dynamic> Details(int ScheduleID)

Upvotes: 0

Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

First of all: when You call ToList() or AsEnumerable() extension methods Your query will be executed immediately, which can impact performance in case of large amount of returned data. And You will not get the advantage of yield return in foreach loop.

Second: why do You use dynamic type? You can define a class for returned data instead of using anonymous type and return this class from Your method. And, I guess, Your method should return only one item or null (if there is no item with specified index) so You can use FirstOrDefault() extension method.

To summarize:

public class YourReturnType
{
    public int ScheduleId {get; set;}
    // other properties
}

public YourReturnType Details(int ScheduleID)
{
    var scheduleItem = db.schedules.FirstOrDefault(f => f.ScheduleId== ScheduleID);

    if (scheduleItem == null) return null;

    return new YourReturnType 
    {
        ScheduleId = scheduleItem.ScheduleId,
        // assign other properties
    };
}

Edits: If You return a collection of some anonymous type and the return type of Your method is dynamic to query this collection You should cast it to appropriate type. If You want to access items by index then You should cast it to IList:

var result = ((IList)Details(SomeId))[index];

Upvotes: 1

When using Enumerable() query that returns a sequence of values do not consume the target data until the query object is enumerated. This is known as deferred execution.

so trying getting the values like this

var result=  x.Details(selectedID).ToList();

Upvotes: 1

Related Questions