如何从视图模型中获得随机记录

本文关键字:随机 记录 模型 视图 | 更新日期: 2023-09-27 17:49:45

我试图将几个参数传递到控制器&根据这些参数,我连接一些表,以过滤我想要的最终结果(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;
       }

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

其他模型;

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

谁能建议一种方法来做到这一点?谢谢你! !

如何从视图模型中获得随机记录

下面的内容应该可以让您开始(没有经过测试,因为我现在没有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
    }
}