I created following viewmodel for getting from my DB all the results from 3 tables in a single list:
public class ResultsVM
{
public Room room { get; set; }
public Hotel hotel { get; set; }
public List<Picture> pictures { get; set; }
}
I'm then passing my View a list of ResultsVM
@model List <ResultsVM>
Now I'm trying to extract from this list a new list with the distinct object Hotel
.
Is there a way to achieve it with Linq or shall I make a loop and compare them one by one?
Assuming that Hotel
objects have unique IDs, you could get distinct hotels with this LINQ query:
List<ResultsVM> results = ...
var hotels = results
.Select(r => r.Hotel)
.GroupBy(h => h.HotelId)
.Select(g => g.First())
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.