I'm trying to get a distinct list to my view.I need to select records from a list randomly and put it in to another list.The following code works..But it contain duplication records..How can I overcome this problem?
Note: the variable "budget" is a parameter passed in to the controller and "model1" is a List of PlanObjectsViewModel
int count = 0;
foreach (var item in model1) { count++; }
List<PlanObjectsViewModel> result = new List<PlanObjectsViewModel>();
Random rand = new Random();
double? temp=0;
while(budget>temp)
{
int randi = rand.Next(0, count);
var nthItem = model1.OrderBy(p => p.Id).Skip(randi).First();
temp += nthItem.Price;
if (!result.Contains(nthItem)) // Think this is the wrong point
{
result.Add(nthItem);
}
}
Aucun commentaire:
Enregistrer un commentaire