How to randomize collection of EF models [duplicate] - c#

This question already has answers here:
Linq to Entities, random order
(12 answers)
Randomize a List<T>
(28 answers)
Closed 9 years ago.
I have a User object that has a collection of Items.
How can I randomize the Items so they dont' appear in the exact same order everytime.
I'm currently ordering by SortOrder (integer), but this will obviously be in the same order everytime.
#foreach(UserItems ui in Model.User.Items.OrderBy(x => x.SortOrder))
{
}

Here's a little trick:
#foreach(UserItems ui in Model.User.Items.OrderBy(x => Guid.NewGuid()))
{
}

Related

I want to loop through List of object in C# using for loop [duplicate]

This question already has answers here:
LINQ select one field from list of DTO objects to array
(5 answers)
How to get only specific field from the list
(2 answers)
Closed 2 years ago.
I have a list of object
my_list = [{ name: 'alex',last_name: 'leda'},{ name: 'john',last_name: 'parsons'}]
I want to loop through my_list using for loop.
I want to get all the last_name from my_list

Query a list and select top 10 values [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);

Linq Distinct() is not working [duplicate]

This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct

Check a list has set of values [duplicate]

This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.
How about
list1.Except(list2).Any();
Try using
[listName].Except(SecondListName).Any();
This should work.

Get distinct parameters from a List [duplicate]

This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).

Categories