简体   繁体   中英

How to get Distinct() by column index of IEnumerable<IList<string>> in Linq

I am returning data row value from as IEnumerable<IList<string>> distinct(). I want to get distinct() values of a column passing index of the column. How can I get this.

    private void AddData(IEnumerable<IList<string>> dataList, IList<string> header)
    {
        try
        {
            int val = header.IndexOf("Package Code");
            //dataList = dataList.Select(x=>x.ToList().Distinct())
                // For the above I have to get distict recod data values 

            foreach (var row in dataList)
            {

            }
        }
        catch
        {
            throw new ArgumentException("Column Names  not matching");
        }
    }

If you just want the distinct values of a single column:

IEnumerable<IList<string>> dataList = ...;

int val = ...;
IEnumerable<string> distincts = dataList.Select(x => x[val]).Distinct();

It isn't exactly clear what you want... some persons often ask for the Distinct() of some fields, but returning full rows... This is the first row of each distinct value of x[val]

IEnumerable<IList<string>> distinctRows = dataList.GroupBy(x => x[val]).Select(x => x.First());

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM