我正在针对数据库运行此查询:
var result = (from leads in dc.T_DM_FactDemandeWebLeads
join demands in dc.T_DM_DimDemandeWebs on leads.DemandeWeb_FK equals demands.DemandeWeb_PK
join temps in dc.T_DM_Temps on demands.DateDemande_FK equals temps.Temps_PK
where leads.longitudeClient != null && (Convert.ToInt32(leads.GeolocDistanceRouteDistrib) > 1000*30) && (temps.Date > new DateTime(2000, 1, 1).Date)
select new Lead
{
lng = leads.longitudeClient,
lat = leads.latitudeClient,
distance = leads.GeolocDistanceRouteDistrib
}).Take(1000000);
问题:此行有问题:
(Convert.ToInt32(leads.GeolocDistanceRouteDistrib) > 1000*30)
as leads.GeolocDistanceRouteDistrib是一个VARCHAR,在某些情况下采用“未知”,导致format exception:
Conversion failed when converting the varchar value 'Unknown' to data type int.
此问题已解决here,但该方法无法转换为SQL。
问题:有没有办法重写查询,以便在执行查询期间完成转换?
答案 0 :(得分:2)
使用this answer中的技术在dbml文件中创建IsNumeric sql函数。 并在查询中使用它。
这样的事情:
join temps in dc.T_DM_Temps on demands.DateDemande_FK equals temps.Temps_PK
where dc.ISNUMERIC(leads.GeolocDistanceRouteDistrib) // Added!
where leads.longitudeClient != null && (Convert.ToInt32(leads.GeolocDistanceRouteDistrib) > 1000*30) && (temps.Date > new DateTime(2000, 1, 1).Date)
select new Lead
答案 1 :(得分:1)
将 .AsEnumerable()添加到您的实体,然后您可以使用所有方法
var result = (from leads in dc.T_DM_FactDemandeWebLeads.AsEnumerable()
join demands in dc.T_DM_DimDemandeWebs on leads.DemandeWeb_FK equals demands.DemandeWeb_PK
join temps in dc.T_DM_Temps on demands.DateDemande_FK equals temps.Temps_PK
where leads.longitudeClient != null && (Convert.ToInt32(leads.GeolocDistanceRouteDistrib) > 1000*30) && (temps.Date > new DateTime(2000, 1, 1).Date)
select new Lead
{
lng = leads.longitudeClient,
lat = leads.latitudeClient,
distance = leads.GeolocDistanceRouteDistrib
}).Take(1000000);