我寫這個SQL查詢LINQ到實體無法識別方法「的Int32 ToInt32(System.Object的)」方法
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 to改寫它,但我總是感到困惑時寫左聯接在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實體無法識別方法「的Int32 ToInt32(System.Object的)」方法,和這種方法不能被翻譯成表達商店。
在
的foreach(詢問變種B)
任何想法是如何解決這一問題?我環顧四周,但其他解決方案,我發現不處理連接
Google你的錯誤。第一頁充滿了解決方案。 – FCin
@FCin,是的,已經做到了。我知道了,但是我有一個允許在其中一個表中有空位的列。第一頁上的這些解決方案都沒有發現這可能是問題所在。 – Chris