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:
original
won't be changed,changed
will 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
IdGeographie
is the three letters "Zon" followed by the string representation of theIdZone
and nothing else. So no spaces, no leading zeroes etc: "Zon4" and not "Zon004", nor "Zon 4"You have two
IQueryables
one for waters and one for zones:Every
Zone
contains an int propertyIdZone
, and a property.IdBatimentNavigation.IdEtablissementNavigation
, which seems to be of typeEtablissement
Furhermore every
Water
has a string propertyGeographieId
in the format "ZonX" where X is an integer number.Now you want to query the
IdBatimentNavigation.IdEtablissementNavigation
of allZones
with anIdZone
that equals the X part of one or more of theWaters
For example: if you have the following
Waters
And you have
Zones
withIdZone
: 4, 42, 30, 7, 22.Then as a result you want the
IdBatimentNavigation.IdEtablissementNavigation
of theZones
withIdZone
: 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.