2015-05-20 48 views
0

我试图根据这些参数将几个参数传递给控制器​​&我加入一些表过滤器我想要的最终结果(model1)。现在我想从model1中选择一些随机记录并将其细节显示给viewmodel。条件是,所选项目的总价格不能超过参数预算...我尝试了这种方法,但我不能做到这一点..如何从视图模型中获得随机记录

在我的控制器我有这个;

public ActionResult planview(Double budget, DateTime startTime, DateTime endTime) 
    { 




     var model = from Ts in db.TimeSpans 
        let time1 = EntityFunctions.CreateTime(Ts.StartTime.Value.Hour, 
               Ts.StartTime.Value.Minute, 
               Ts.StartTime.Value.Second) 

        let time2 = EntityFunctions.CreateTime(Ts.EndTime.Value.Hour, 
              Ts.StartTime.Value.Minute, 
              Ts.StartTime.Value.Second) 
        where (time1 > startTime.TimeOfDay && endTime.TimeOfDay > time1) || (time1 < startTime.TimeOfDay && endTime.TimeOfDay < time2) || (time1 < startTime.TimeOfDay && endTime.TimeOfDay > time2) 
     select Ts; 

     var model1 = from Ts1 in model 
        join ob in db.Objects on Ts1.ObjectId equals ob.Id 
        select new PlanObjectsViewModel { 

         Id=ob.Id, 
         StartTime=Ts1.StartTime, 
         EndTime=Ts1.EndTime, 
         LocationId=Ts1.LocationId, 
         Name=ob.Name, 
         Price=ob.Price, 
         Description=ob.Description, 
         Image=ob.Image, 
         Type=ob.Type 
        }; 

     int count = 0; 
     foreach (var item in model1) 
     { 

      count++; 
     } 

     Random rand = new Random(); 
     int temp=0; 

     while(budget>temp) 
     { 
      int randi = rand.Next(0, count); 
      foreach(var item in model1) 
      if(item.Id==randi) 
       //select that record ; 
       // select new PlanObjectsViewModel { 

         // Id=ob.Id, 
         // StartTime=Ts1.StartTime, 
         // EndTime=Ts1.EndTime, 
         // LocationId=Ts1.LocationId, 
         // Name=ob.Name, 
         // Price=ob.Price, 
         // Description=ob.Description, 
         // Image=ob.Image, 
         // Type=ob.Type 
        }; 
       //temp+=temp; 
     } 

我的viewmodel是;

public class PlanObjectsViewModel 
{ 

    public int? Id { get; set; } 

    public DateTime? StartTime { get; set; } 

    public DateTime? EndTime { get; set; } 

    public int? LocationId { get; set; } 

    public String Name { get; set; } 

    public Double? Price { get; set; } 

    public String Description { get; set; } 

    public String Image { get; set; } 

    public int? Type { get; set; } 
} 

其他型号;

public partial class TimeSpan 
{ 
    public int Id { get; set; } 
    public Nullable<System.DateTime> StartTime { get; set; } 
    public Nullable<System.DateTime> EndTime { get; set; } 
    public Nullable<System.DateTime> StartDate { get; set; } 
    public Nullable<System.DateTime> EndDate { get; set; } 
    public Nullable<int> LocationId { get; set; } 
    public Nullable<int> ObjectId { get; set; } 

    public virtual LocationInfo LocationInfo { get; set; } 
    public virtual Object Object { get; set; } 
} 

public partial class Object 
{ 
    public Object() 
    { 
     this.ObjectCategories = new HashSet<ObjectCategory>(); 
     this.TimeSpans = new HashSet<TimeSpan>(); 
     this.Branches = new HashSet<Branch>(); 
     this.Events = new HashSet<Event>(); 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public Nullable<double> Price { get; set; } 
    public string Description { get; set; } 
    public string Image { get; set; } 
    public Nullable<int> Type { get; set; } 

    public virtual BaseType BaseType { get; set; } 
    public virtual ICollection<ObjectCategory> ObjectCategories { get; set; } 
    public virtual ICollection<TimeSpan> TimeSpans { get; set; } 
    public virtual ICollection<Branch> Branches { get; set; } 
    public virtual ICollection<Event> Events { get; set; } 
} 

任何人都可以提出一个方法来做到这一点?谢谢!!

+0

问题temp中存在错误+ = item.Price .. –

回答

0

下面应该让你开始(没有测试,因为我现在没有在我面前的IDE)。而不是迭代项目从您的列表中获取一个随机项目,并将其添加到您返回的结果,这是这些项目的列表。

注意:请注意,如果您的模型中的项目总数少于预算,您将以紧密的循环结束。

//List of items to return 
List<PlanObjectsViewModel> result = new List<PlanObjectsViewModel>(); 

Random rand = new Random(); 
int temp=0; 

while(budget>temp) 
{ 
    //Get random item within selection 
    int randi = rand.Next(0, count); 
    var nthItem = model1.Skip(randi).First(); 

    //Remove this test if you are happy adding duplicates 
    if (!result.Find(nthItem) 
    { 
     result.Add(nthItem); 

     //Update temp with calculation from nthItem just added 
    } 
} 
+0

这给出了一个异常! “跳过”方法仅支持LINQ to Entities中的排序输入。方法'OrderBy'必须在方法'Skip'之前调用。 –