我试图根据这些参数将几个参数传递给控制器&我加入一些表过滤器我想要的最终结果(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; }
}
任何人都可以提出一个方法来做到这一点?谢谢!!
问题temp中存在错误+ = item.Price .. –