简体   繁体   English

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

[英]LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method

I wrote this SQL query 我写了这个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

and want to rewrite it using LINQ, but I always get confused when writing left joins in LINQ, so I decided to use a convertor and went with Linqer and this is what it produced 并想使用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
}

so when I put everything together in code 所以当我把所有东西都放在代码中

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;
    }

I get the error 我得到错误

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'Int32 ToInt32(System.Object)'方法,并且该方法无法转换为商店表达式。

at the

foreach(var b in query) foreach(查询中的var b)

Any idea's on how to fix this? 有想法该怎么解决这个吗? I have looked around but other solutions I found aren't dealing with joins 我环顾四周,但发现其他解决方案与联接无关

Linq doesn't support the Convert.ToInt32(dscx.StateCountyID) method inside the query because this will need to be converted to sql for execution on the db. Linq不支持查询内部的Convert.ToInt32(dscx.StateCountyID)方法,因为需要将其转换为sql才能在数据库上执行。

I am not sure what datatype StateCountyID is in both tables but you will need to use some sql compatible conversion method like SqlFunctions.StringConvert() to get them to the same datatype. 我不确定两个表中的数据类型为StateCountyID,但是您将需要使用sqlFunctions.StringConvert()之类的与SQL兼容的转换方法,以将它们转换为相同的数据类型。

Convert them to Strings 将它们转换为字符串

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() } 

or use the below: 或使用以下方法:

dscx.StateCountyID equals SqlFunctions.Convert(county.StateCountyID)

I figured out why I was getting that error. 我弄清楚了为什么会出现该错误。 It was because I had StateCountyID as nullable in my DepartmentStateCountyXREF table. 这是因为我的DepartmentStateCountyXREF表中的StateCountyID可为空。 Once I changed the StateCountyID column from a nullable and then removed the Convert.ToInt32, everything worked. 一旦我从一个可为空的字段更改了StateCountyID列,然后删除了Convert.ToInt32,一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression 将字符串转换为int时,特定的linq异常。 LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;m - Specific linq exception when converting string to int. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' m LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression linq to entities无法识别int32 toint32的方法 - linq to entities does not recognize the method int32 toint32 LINQ to Entities无法识别方法&#39;Int32 ToInt32(Boolean)&#39; - LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' LINQ to Entities 无法识别方法 &#39;Int32 Int32(System.String)&#39; 方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method LINQ to Entities无法识别方法&#39;Int32 - LINQ to Entities does not recognize the method 'Int32 LINQ to Entities无法识别方法&#39;Boolean Contains [Int32] - LINQ to Entities does not recognize the method 'Boolean Contains[Int32]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM