I have a list of gelolocations
. I want to perform 2 conditions on the list and select the ones which satisfy those conditions. I cannot figure out how to do that.
public class GeolocationInfo
{
public string Postcode { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
var geolocationList = new List<GeolocationInfo>(); // Let's assume i have data in this list
I want to perform multiple conditions on this list geolocationList
.
I want to use FirstOrDefault
on this list on the conditions that PostCode
property matching with the one supplied and Longitude, lattitude are not null.
geolocationList .FirstOrDefault(g => g.PostCode == "AB1C DE2");
// I want to add multiple conditions like g.Longitude != null && g.Lattitude != null in the same expression
I want to build this conditions
outside and pass it as a parameter to FirstOrDefault
. e.g like building a Func<input, output>
and passing this into.
Thanks for your response guys. It helped me think in the right way.
I did like this.
Func<GeolocationInfo, bool> expression = g => g.PostCode == "ABC" &&
g.Longitude != null &&
g.Lattitude != null;
geoLocation.FirstOrDefault(expression);
It worked and code is much better.