我遇到了LINQ to Entities查询的问题,我正在加入四个表但是下面的连接列没有相同数据类型的两个表是查询。
var qry = (from i in dc.INTERNETDATAs
join c in dc.COVERS on Convert.ToInt32(i.COVERTYPE) equals c.COV_TYPE
join b in dc.BENEFITS on c.COVERID equals b.COVERID
join p in dc.POLICies on i.REFERENCENO equals p.BACS_POLNO into grp
where i.DATELOADED >= startDate
&& i.DATELOADED < endDate
&& i.CAMPAIGNTYPE == campaignType
&& c.COVERTYPEID == coverTypeId
select i).DefaultIfEmpty();
在第一次加入上面的代码中
join c in dc.COVERS on Convert.ToInt32(i.COVERTYPE) equals c.COV_TYPE
发出以下错误
InnerException:System.NotSupportedException Message = LINQ to Entities无法识别方法'System.String ToString()'方法,而且此方法不能 翻译成商店表达。 源= System.Data.Entity的
我怎样才能做到这一点?
答案 0 :(得分:3)
假设i.COVERTYPE是一个字符串,请避免使用Convert.ToInt32()。请在* c.COV_TYPE *上使用SqlFunctions.StringConvert
。它来自System.Data.Objects.SqlClient
,可以像:
var qry = (from i in dc.INTERNETDATAs
join c in dc.COVERS on i.COVERTYPE equals SqlFunctions.StringConvert(c.COV_TYPE)
join b in dc.BENEFITS on c.COVERID equals b.COVERID
join p in dc.POLICies on i.REFERENCENO equals p.BACS_POLNO into grp
where i.DATELOADED >= startDate
&& i.DATELOADED < endDate
&& i.CAMPAIGNTYPE == campaignType
&& c.COVERTYPEID == coverTypeId
select i).DefaultIfEmpty();
答案 1 :(得分:0)
var qry = (from i in dc.INTERNETDATAs
join c in dc.COVERS on ConvertID(i.COVERTYPE) equals c.COV_TYPE
join b in dc.BENEFITS on c.COVERID equals b.COVERID
join p in dc.POLICies on i.REFERENCENO equals p.BACS_POLNO into grp
where i.DATELOADED >= startDate
&& i.DATELOADED < endDate
&& i.CAMPAIGNTYPE == campaignType
&& c.COVERTYPEID == coverTypeId
select i).DefaultIfEmpty();
public int CheckId(string id)
{
Int32 CoverType=Convert.ToInt32(id);
return CoverType ;
}
Linq to Entities 不支持Convert.ToInt(system.string)
,无法将其转换为Sql数据类型。尝试使用SqlFunctions
比较您的两个值,或更改您的datatype
。
尝试将两者都转换为doubles for comparison purpose
。