开发者

How to remove duplicate values from a IList, c#

I have an ilist with 6 records, something like this

1st Row

time 11:00

location:

B开发者_如何学Cangalore

2nd Row time 11:00

Location NULL

....

I have to eliminate the 2nd row which has location null for the same time (11:00)

Like this, I will be having thousands of records, from which i need to eliminate this.

any solution ?


You could 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()


        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;
        }


You might as well simply execute a "for" statement, and remove elements that don't qualify (with the step variable carefully "not-incremented" whenever a remove is performed).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜