繁体   English   中英

查找已删除的项目但抛出异常

[英]Find deleted item but throw exception

我有两个实体 class 如下:

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

对于我有 FormObject 定义如下:

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

由于某种原因,前端将在更新期间传递 null 值在删除 SessionLogs 项的工作期间,我想找出删除了哪些日志详细信息,我尝试在以下工作:

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

当我 go 到 findDeletedLog.First() 时,它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'. 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'. 即使我变成列表也不行,我能知道如何解决吗?

我认为问题在于 LINQ 不支持在此本地内存集合上使用Any 请注意,LINQ 提供程序需要将您的代码转换为有效的 sql。

尝试这个:

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

    // ...
}

暂无
暂无

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

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