Frage

I work to select stations according to the following conditions code

var NStations = stations.AsEnumerable()
                        .Where(r => r.Field<double>("Distance") < SP_Radius)
                        .OrderBy(r => r.Field<double>("Distance"))
                        .CopyToDataTable();

The problem if no data matches the first condition, I will get error stop the program.

How can I avoid this problem where no datarow is retrieved?

ERROR :

The source contains no DataRows.

It check the first condition and one go to the second condition to order it give the above error.

War es hilfreich?

Lösung

You can separate your query into two parts:

var result = stations.AsEnumerable()
             .Where(r => r.Field<double>("Distance") < SP_Radius);

if(result.Any())
{ 
   var NStations = result.OrderBy(r => r.Field<double>("Distance"))
                         .CopyToDataTable();
} 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top