LINQ to Entities无法识别方法'Int32 ToInt32(System.Object)'方法

时间:2017-09-23 14:02:54

标签: c# linq

我写了这个SQL查询

select 
    acc.DepartmentID,
    dept.DepartmentName,
    dept.DepartmentDivision,
    county.CountyName,
    sp.StateProvinceID
from [AccountDepartmentXREF] acc
inner join [Department] dept on dept.DepartmentID = acc.DepartmentID
left join [DepartmentStateCountyXREF] dscx on dscx.DepartmentID = acc.DepartmentID
left join [StateCounty] county on county.StateCountyID = dscx.StateCountyID
inner join [StateProvince] sp on sp.StateProvinceID = dept.StateProvinceID
where acc.AccountID = 1

并希望使用LINQ重写它,但在LINQ中编写左连接时总是感到困惑,所以我决定使用转换器并使用Linqer,这就是它生成的内容

from acc in db.AccountDepartmentXREF
join dscx in db.DepartmentStateCountyXREF on acc.DepartmentID equals dscx.DepartmentID into dscx_join
from dscx in dscx_join.DefaultIfEmpty()
join county in db.StateCounty on new { StateCountyID = Convert.ToInt32(dscx.StateCountyID) } equals new { StateCountyID = county.StateCountyID } into county_join
from county in county_join.DefaultIfEmpty()
join sp in db.StateProvince on acc.Department.StateProvinceID equals sp.StateProvinceID
where
acc.AccountID == 1
select new {
acc.DepartmentID,
acc.Department.DepartmentName,
acc.Department.DepartmentDivision,
CountyName = county.CountyName,
sp.StateProvinceID
}

所以当我把所有内容放在代码中时

public List<DepartmentList> GetDepartmentsByAccountID(string email)
    {
        HWC = new HWCEntities();
        List<DepartmentList> result = new List<DepartmentList>();

        int id = CurrentUserID(email);

        List<AccountDepartmentXREF> adx = HWC.AccountDepartmentXREFs.Where(w => w.AccountID == id).ToList();

        foreach(var a in adx)
        {
            var query = from acc in HWC.AccountDepartmentXREFs
                        join dscx in HWC.DepartmentStateCountyXREFs on acc.DepartmentID equals dscx.DepartmentID into dscx_join
                        from dscx in dscx_join.DefaultIfEmpty()                                
                        join county in HWC.StateCounties on new { StateCountyID = Convert.ToInt32(dscx.StateCountyID) } equals new { StateCountyID = county.StateCountyID } into county_join
                        from county in county_join.DefaultIfEmpty()
                        join sp in HWC.StateProvinces on acc.Department.StateProvinceID equals sp.StateProvinceID
                        where
                          acc.AccountID == a.AccountID
                        select new
                        {
                            acc.DepartmentID,
                            acc.Department.DepartmentName,
                            acc.Department.DepartmentDivision,
                            CountyName = county.CountyName,
                            sp.StateProvinceID
                        };

            foreach(var b in query)
            {
                result.Add(new DepartmentList
                {
                    DepartmentID = b.DepartmentID,
                    DepartmentName = b.DepartmentName,
                    StateProvinceID = b.StateProvinceID,
                    DivisionName = b.DepartmentDivision,
                    CountyName = b.CountyName
                });
            }
        }

        return result;
    }

我收到错误

  

LINQ to Entities无法识别方法'Int32 ToInt32(System.Object)'方法,并且此方法无法转换为商店表达式。

  

foreach(查询中的var b)

关于如何解决这个问题的想法?我环顾四周,但我找到的其他解决方案并没有处理连接

3 个答案:

答案 0 :(得分:0)

Linq不支持查询中的Convert.ToInt32(dscx.StateCountyID)方法,因为这需要转换为sql以便在db上执行。

我不确定两个表中的数据类型StateCountyID是什么,但您需要使用一些sql兼容的转换方法,如SqlFunctions.StringConvert(),以使它们具有相同的数据类型。

答案 1 :(得分:0)

将它们转换为字符串

DataFrame

或使用以下内容:

from acc in HWC.AccountDepartmentXREFs
                        join dscx in HWC.DepartmentStateCountyXREFs on acc.DepartmentID equals dscx.DepartmentID into dscx_join
                        from dscx in dscx_join.DefaultIfEmpty()                                
                        join county in HWC.StateCounties on new { StateCountyID = dscx.StateCountyID.ToString() } equals new { StateCountyID = county.StateCountyID.ToString() } 

答案 2 :(得分:0)

我弄明白为什么我会收到这个错误。这是因为我在StateStateCountyXREF表中将StateCountyID视为可为空。一旦我从Nullable中更改了StateCountyID列,然后删除了Convert.ToInt32,一切正常。