我试图将几个参数传递给控制器&根据那些参数我加入一些表过滤器我想要的最终结果(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 :(得分: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
}
}