Domanda

I am getting datarowview through the following code

DataRowView s = grdSampleData.GetRow(grdSampleData.FocusedRowHandle) as DataRowView;

how can i check here that all values in s.Row.ItemArray is distinct. If value is null or empty string it should skip the distinct nature ie there can be multiple null values or empty string in ItemArray but no other same values.

È stato utile?

Soluzione

you can use LINQ GroupBy to check the distinct:

 bool flag = s.row.ItemArray.Where(x => x != null)
            .Where(x => (x is string) && !string.IsNullOrEmpty((string) x))
            .GroupBy(x => x).Any(g => g.Count() > 1);

How it works:

To simplify, assume your item array has: [1, 1, 3, 3]. GroupBy(x => x) will group your array in two group:

[ [1, 1], [3, 3] ]

To know whether it is distinct or not, you just check whether any group has Count > 1, if yes, it is not distinct. Example in here is not distinct

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top