如何在LINQ to Entities Join中使用Convert.Int32()方法?

时间:2013-09-06 11:08:25

标签: c# linq linq-to-entities

我遇到了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的

我怎样才能做到这一点?

2 个答案:

答案 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