简体   繁体   中英

c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange

I am receiving this error when running the code below:

Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<StatisticsGenerator.RosterSummaryData_Subject_Local>

I have two entity classes: RosterSummaryData_Subject_Local and RosterSummaryData_Subject_Local_Bands. Subject_Local_Bands contains a foreign key reference to the primary key in Subject_Local. I want to get subjLocal so I can execute a RemoveRange on the class, passing to the method the variable subjLocal like so:

customerContext.RosterSummaryData_Subject_Local.RemoveRange(subjLocal);

This works perfect, but I then want to do the same to Local_Bands with the variable subjLocalBands:

customerContext.RosterSummaryData_Subject_Local_Bands.RemoveRange(subjLocalBands);

I understand that the error is occurring on the line where I set subjLocalBands and try to compare the foreign key int to the IQueryable variable subjLocal, but I don't know how to get the proper IQueryable in this situation where the foreign key fkSummarySubjectLocalID in Local_Bands equals that in subjLocal. Is there an easy way to do this because I am sort of new to entity and Linq and just don't know enough regarding this scenario.

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

In this line of code, looks like you are equating integer and RosterSummaryData_Subject_Local . You can use extension method Where to get he object with ID as you want.

If subjLocal will contain only one element, you can add a call to FirstOrDefault to previous statement and then use something like these:

s.fkSummarySubjectLocalID == subjLocal.ID

If subjLocal will have multiple entries, then something like this could be used in the Where call:

subjLocal.Any(x => x.ID == s.fkSummarySubjectLocalID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM