I have two entity class as below :
class SessionMaster
{
public int ID { get; set; }
public string SessionTag { get; set; }
public List<SessionLog> SessionLogs { get; set; }
}
class SessionLog
{
public long ID { get; set; }
public int SessionMasterID { get; set; }
public string LogMessage { get; set; }
}
For and I have FormObject define as below :
class FO_SessionMaster
{
public int? ID { get; set; }
public string SessionTag { get; set; }
public List<FO_SessionLog> fo_SessionLogs { get; set; }
}
class FO_SessionLog
{
public long? ID { get; set; }
public int? SessionMasterID { get; set; }
public string LogMessage { get; set; }
}
for some reason, Front end will pass null value during update During work on delete Item of SessionLogs, I want to find out which log detail is deleted, I try to work on below :
public void UpdateSession (FO_SessionMaster formData)
{
var findDeletedLog = dBContext.Set<SessionLog>().Where(w => w.SessionMasterID == formData.ID && !formData.fo_SessionLogs.Any(a => (long)a.ID == w.ID));
var getFirstRecord = findDeletedLog.First();
}
when I go to findDeletedLog.First(), it throw exception : System.InvalidOperationException: .... 'The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
Even I turn into list is not work, Can I know how to solve it ?
CodePudding user response:
I think the problem is that LINQ does not support using Any
on this local in-memory collection. Note that the LINQ provider needs to translate your code to valid sql.
Try this:
public void UpdateSession (FO_SessionMaster formData)
{
List<long> sessionLogIdList = formData.fo_SessionLogs
.Select(sl => (long) a.ID)
.ToList();
var findDeletedLog = dBContext.Set<SessionLog>()
.Where(w => w.SessionMasterID == formData.ID && !sessionLogIdList.Contains(w.ID));
// ...
}