![](/img/trans.png)
[英]Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.iQueryable<int>'
[英]c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange
運行以下代碼時,我收到此錯誤:
Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<StatisticsGenerator.RosterSummaryData_Subject_Local>
我有兩個實體類:RosterSummaryData_Subject_Local和RosterSummaryData_Subject_Local_Bands。 Subject_Local_Bands包含一個對Subject_Local中的主鍵的外鍵引用。 我想獲取subjLocal,以便可以在類上執行RemoveRange,將變量subjLocal傳遞給方法,如下所示:
customerContext.RosterSummaryData_Subject_Local.RemoveRange(subjLocal);
這很完美,但是我接下來想對sub_LocalBands變量使用Local_Bands:
customerContext.RosterSummaryData_Subject_Local_Bands.RemoveRange(subjLocalBands);
我知道錯誤發生在我設置subjLocalBands並嘗試將外鍵int與IQueryable變量subjLocal進行比較的行上,但是在這種情況下,如果Local_Bands中的外鍵fkSummarySubjectLocalID是不正確的,我不知道如何獲取正確的IQueryable等於subjLocal中的值。 有沒有一種簡單的方法可以執行此操作,因為我是實體和Linq的新手,只是對這種情況不了解。
var subjLocal = customerContext.RosterSummaryData_Subject_Local.Where(s =>
(s.fkRosterSetID == 0) &&
(statsInfo.TestInstanceIDsList.Contains(s.fkTestInstanceID)) &&
(s.fkTestTypeID == statsInfo.TestTypeID) &&
(statsInfo.SchoolYearIDsList.Contains(s.fkSchoolYearID)) &&
(s.fkRosterTypeID == 1) &&
(s.fkSchoolID == 0) &&
(s.fkDepartmentID == 1) &&
(s.fkCourseID == 1) &&
(s.fkPeriodID == 1) &&
(statsInfo.DemoCatIDsList.Contains(s.fkDemoCommonCategoryID)) &&
(statsInfo.DemoCodeIDsList.Contains(s.fkDemoCommonCodeID)) &&
(statsInfo.TestSubjectIDsList.Contains(s.fkTest_SubjectID)));
var subjLocalBands = customerContext.RosterSummaryData_Subject_Local_Bands.Where(s => s.fkSummarySubjectLocalID == subjLocal));
s.fkSummarySubjectLocalID == subjLocal
在這行代碼中,您好像將integer
和RosterSummaryData_Subject_Local
。 您可以使用擴展方法Where
根據需要獲取帶有ID的對象。
如果subjLocal
只包含一個元素,則可以在前面的語句中添加對FirstOrDefault
的調用,然后使用以下命令:
s.fkSummarySubjectLocalID == subjLocal.ID
如果subjLocal
將具有多個條目,則可以在Where
調用中使用類似的方法:
subjLocal.Any(x => x.ID == s.fkSummarySubjectLocalID)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.