2

我在使用 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()”方法,并且该方法无法转换为存储表达式。源=系统.数据.实体

我怎样才能做到这一点?

4

2 回答 2

3

假设 i.COVERTYPE 是一个字符串,避免使用 Convert.ToInt32()。SqlFunctions.StringConvert改为在 *c.COV_TYPE*​​ 上使用。它来自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();
于 2013-09-06T11:38:11.077 回答
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.

于 2013-09-06T12:30:33.070 回答