public static void WriteVehicleRegistrationNumberToFile(string file) { //string file = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"); using (StreamWriter sw = new StreamWriter(file)) { using (var context = new ControlExpertEntities()) { var ds = context.VehicleClaims.Select(vc => vc.VehicleRegistionNumber); // foreach, get tree and try to get circle. foreach (var d in ds) { if (IsValidVehicleRegistrationNumber(d)) { sw.WriteLine(d); } } } } }