在控制器中访问查询返回列表的值

时间:2013-10-10 06:29:49

标签: c# asp.net asp.net-mvc asp.net-mvc-4

这是我的日程安排课程

[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; }
    }
}

这是我的上下文类

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

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

这是我的函数返回查询结果:

 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();
 }

当我在控制器中调用它时:

var result= x.Details(selectedID);

结果变量显示null值,尽管查询返回值。

如何在控制器中访问查询结果?

请提供任何链接,参考,提示。

3 个答案:

答案 0 :(得分:1)

使用返回值序列的Enumerable()查询时,在枚举查询对象之前不会使用目标数据。这称为延迟执行。

所以尝试获取像这样的值

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

答案 1 :(得分:1)

首先:当您调用ToList()AsEnumerable()扩展方法时,您的查询将立即执行,这会在大量返回数据的情况下影响性能。而且你不会在 foreach 循环中获得收益率回报的优势。

第二:为什么使用dynamic类型?您可以为返回的数据定义类,而不是使用匿名类型,并从Your方法返回此类。并且,我猜,您的方法应该只返回一个项目或 null (如果没有指定索引的项目),那么您可以使用FirstOrDefault()扩展方法。

总结:

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
    };
}

编辑:如果您返回某个匿名类型的集合,并且您的方法的返回类型是动态的,则查询此集合您应该将其强制转换为适当的类型。如果要按索引访问项目,则应将其强制转换为IList:

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

答案 2 :(得分:0)

Details方法的返回类型必须是IEnumerable&lt;动态&gt;。

public IEnumerable<dynamic> Details(int ScheduleID)
相关问题