How to remove duplicate values ​​from IList, C #

I have an ilist with 6 entries, something like this

1st row

time 11:00

Location:

Bangalore

Second line time 11:00

NULL Location

....

I need to eliminate a second line that has a zero location at the same time (11:00)

Thus, I will have thousands of entries from which I need to eliminate this.

any solution?

+3
source share
3 answers

You can do something like:

list.GroupBy(x=>x.Time)
    //from the grouped items select the first one that doesn't have an empty location - you'll have null here if none is found
    .Select(x=>x.FirstOrDefault(y=>!string.IsNullOrEmpty(y.Location)))
    .ToList()
+4
source
        public List<Row> GetRowsWthoutDuplicates(List<Row> source)
        {
            List<Row> filteredRows = new List<Row>(source);

            foreach (Row row in source)
            {
                if (!filteredRows.Exists(r => r.Time == row.Time))
                {
                    filteredRows.Add(row);
                }
            }

            return
                filteredRows;
        }
+1
source

"" , ( " " ).

0

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


All Articles