mardi 19 mai 2015

How to get random records from a viewmodel

I'm trying to pass several parameters in to the controller & according to those parameters i join some tables a filter the final result i want (model1). Now i want to select some random records from the model1 and get their details to viewmodel. the condition is , total price of the selected items can't be more than parameter budget...I tried this in several ways but i couldn't do it..

in my controller i have this;

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

my viewmodel is;

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

other models;

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

and

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

can anyone suggest a way to do this?Thank you!!




Aucun commentaire:

Enregistrer un commentaire