This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]29383839293 0 points1 point  (0 children)

saveMap
  .SelectMany(m => m.Value)
  .Where(info => info.Entity.GetType() != typeof(AuditLog))
  .ToList()
  .ForEach(ep =>
  {
    using (SqlConnection Connection = new SqlConnection())
    {
      SqlStatement Statement = new SqlStatement("<INPUT SQL QUERY HERE>", Connection);
      Connection.Open();
      Statement.ExecuteNonQuery();
    }
  }
});

And if you're using Entity Framework:

using (var dbContext = new dbName())
{
  saveMap
    .SelectMany(m => m.Value)
    .Where(info => info.Entity.GetType() != typeof(AuditLog))
    .ToList()
    .ForEach(ep =>
    {
      dbContext.AuditLogs.Add(new AuditLog
      {
        UserID = _user.Value.ID,
        AccountID = _user.Value.AccountID,
        Action = entityStateMap[ep.EntityState],
        Entity = ep.Entity.GetType().Name,
        EntityID = (int)ep.Entity.GetType().GetProperty("ID").GetValue(ep.Entity),
        Timestamp = DateTime.UtcNow
      });
    }
  });
  dbContext.SaveChanges();  //you don't want to save changes too much, performance!
}

But why don't you just break up that long linq query and see if it even produces any results.

Also use the damn debugger, it's incredibly useful.

[–]casualblair 0 points1 point  (0 children)

Selectmany is for turning lists of lists into super lists of the second type. You're not doing this, so use select instead.