I'm getting this error :
Conversion failed when converting the varchar value 'Zon7' to data type int.
establishments = GetEstablishments(waters.Select(t => ReplaceZonToEmptyString(t.IdGeographie)));
public static int ReplaceZonToEmptyString(string zoneId)
{
zoneId.Replace("Zon", string.Empty);
var sbInput = zoneId.Replace(" ", string.Empty);
return Convert.ToInt32(sbInput.ToString());
}
public static IQueryable<Etablissement> GetEstablishments(IQueryable<int> ids)
{
return from zone in entities.Zones
where ids.Contains(zone.IdZone)
select zone.IdBatimentNavigation.IdEtablissementNavigation;
}
var result = establishments.ToList();
in database i have a column of type varchar the column name is 'IdGeographie' with values that start with 'Zon', something like this "ZonXXXX"
What do you think the following code will do:
originalwon't be changed,changedwill equal "Hellxx, Wxxrld".So you should change your ReplaceZonToEmptyString:
Alas this will only work on IEnumerable, not on IQueryable
A better solution:
This solution only works if
IdGeographieis the three letters "Zon" followed by the string representation of theIdZoneand nothing else. So no spaces, no leading zeroes etc: "Zon4" and not "Zon004", nor "Zon 4"You have two
IQueryablesone for waters and one for zones:Every
Zonecontains an int propertyIdZone, and a property.IdBatimentNavigation.IdEtablissementNavigation, which seems to be of typeEtablissementFurhermore every
Waterhas a string propertyGeographieIdin the format "ZonX" where X is an integer number.Now you want to query the
IdBatimentNavigation.IdEtablissementNavigationof allZoneswith anIdZonethat equals the X part of one or more of theWatersFor example: if you have the following
WatersAnd you have
ZoneswithIdZone: 4, 42, 30, 7, 22.Then as a result you want the
IdBatimentNavigation.IdEtablissementNavigationof theZoneswithIdZone: 42 any 7 (in any order)Why not join waters and zones?
A better solution would be to try to remove the "Zon" part from IdGeographie, and Parse the remaining XXXX to an int. Alas there is no function that can perform the parsing AsQueryable.