This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 5 years ago.
I have an array of an object called storing, some of the object's attribute (skuID) is the same with some of the other storing with different (storingID), how do i make my array distinct depending on their (skuID)?
Storing(string storingID, skuID, storageID, price, expiry)
I have tried this but it doesn't work:
List<storing> storingAll = (List<storing>)Session["storingAll"];
List<storing> displayedStoring = storingAll.Distinct().ToArray();
storingAll.GroupBy(x=>x.skuID).Select(group=>group.First());
Note that this solution uses no external libraries. Simple group by the first occurrence of skuID.
Reference:
How to get a distinct list from a List of objects?
Related
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);
This question already has answers here:
Check if a string contains an element from a list (of strings)
(12 answers)
Closed 8 years ago.
I'm this expression to search inside a list of objects by a specific property:
var result = myObject.Where(o => o.SearchString.Contains(searchValue));
It works good for a single value. The searchValue is a string passed by the user. The user can pass a single word or many words separeted by spaces. Is there any way to filter the objects that contains any of the passed words?
I could do this with a loop, searching a new word in previous results, but it doesn't seem very elegant.
myObject.Where(o => words.Any(o.SearchString.Contains))
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.
This question already has answers here:
Is there a good LINQ way to do a cartesian product?
(3 answers)
How can I compute a Cartesian product iteratively?
(4 answers)
Closed 8 years ago.
I have a list that contains further sub lists and i have objects stored in the sub list.
I want to generate all possible combinations of all elements.
e.g. we have a list that contains two list L1,L2 and all have different objects stored in the for example L1 contains {obj1,obj2}
L2 contains {obj3,obj4}
then the result should come in the form of
{obj1,obj3}
{obj1,obj4}
{obj2,obj3}
{obj2,obj4}
all lists are being generated dynamically. so the solution should be generic irrespective of count of elements in main list and sub list
L1.SelectMany(l1 => L2.Select(l2 => Tuple.Create(l1, l2))).ToList();
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).