How to get distinguishing values ​​from (Of T) using Linq

I have a List (Of Hardware) - The list is called HWModels

Class equipment has the following properties:

  • ModelName
  • Status
  • CPUStatus
  • Memorystatus
  • Diskstatus

The list is populated by reading the CSV file, after filling it I want to return individual records based on ModelName

I tried to do it as follows:

 (From a In HWModels Select a.ModelName).Distinct 

But this is not correct, because I get a list of only ModelName and nothing more.

How do I get the Distinct function to return all other class members to a list?

+4
source share
2 answers

LINQ to Objects does not provide anything that could be done "clearly projected." You can group by name and then take the first element in each group, but it's pretty ugly.

My MoreLINQ provides the DistinctBy method, although in C # you would use:

 var distinct = HWModels.DistinctBy(x => x.ModelName).ToList(); 

VB supposedly will be something like

 Dim distinct = HWModels.DistinctBy(Function(x) x.ModelName).ToList 

Sorry for any syntax errors though :(

+12
source

This will group your objects using the preferred property and then select the first one, removing duplicates.

  Dim newlist = HWModels.GroupBy(Function(x) x.ModelName).Select(Function(x) x.First).ToList 
+7
source

Source: https://habr.com/ru/post/1400710/


All Articles