views:

1749

answers:

2

Hello everyone I'm currently having 2 issues with the code below:

  1. Upon return of result1 I'm trying to complete a check to see if it is != null and if it is't it will begin to delete the records selected. The issue is that even when result1 returns nothing and defaults the if statement doesn't pick this up so I guess I'm missing something but what?

  2. I'm wishing to return only values which are over 10 mintues old (this will later be scaled to 12 hours) to do this I'm checking against a.DateTime which is a DateTime value stored in a database. However if i use the <= or >= operators it doesn't work so again what am I missing?

    DateTime dateTime = DateTime.Now.Subtract(new TimeSpan(0, 0, 10, 0));
    
    
    var result1 = (from a in cpuInfo
                      where a.DateTime <= dateTime
                      select a).DefaultIfEmpty(null);
    
    
    if (result1 != null)
    {            
        foreach (TblCPUInfo record1 in result1)
        {
                localDB.TblCPUInfo.DeleteOnSubmit(record1);
                localDB.SubmitChanges();
        }
    }
    
+3  A: 

DefaultIfEmpty will return a single item with the content you provided, so in your case a collection with a single value "null".

You should check for elements in the collection using the Any() extension method. In your case:

DateTime dateTime = DateTime.Now.Subtract(new TimeSpan(0, 0, 10, 0));

var result1 = from a in cpuInfo
                  where a.DateTime <= dateTime
                  select a;

if (result1.Any())
{            
    foreach (TblCPUInfo record1 in result1)
    {
            localDB.TblCPUInfo.DeleteOnSubmit(record1);
            localDB.SubmitChanges();
    }
}

But if this is really your code, you can skip the Any() check completely, because the foreach loop will not run if there are no elements in result1.

Philippe Leybaert
+3  A: 

Philippe has talked about the sequence side of things - although you don't even need the call to Any(). After all, if there are no changes the loop just won't do anything.

Do you really want to submit the changes on each iteration? It would probably make more sense to do this once at the end. Additionally, you can use DateTime.AddMinutes to make the initial "10 minutes ago" simpler, and if you're only filtering by a Where clause I'd use dot notation.

After all these changes (and making the variable names more useful), the code would look like this:

DateTime tenMinutesAgo = DateTime.Now.AddMinutes(-10);

var entriesToDelete = cpuInfo.Where(entry => entry.DateTime <= tenMinutesAgo);

foreach (var entry in entriesToDelete)
{
    localDB.TblCPUInfo.DeleteOnSubmit(entry);
}
localDB.SubmitChanges();

Now, as for why <= isn't working for you... is it possible that you need the UTC time instead of the local time? For example:

DateTime tenMinutesAgo = DateTime.UtcNow.AddMinutes(-10);

If that still isn't working, I suggest you have a look at the generated query and play with it in a SQL tool (e.g. Enterprise Manager or SQL Server Management Studio) to work out why it's not returning any results.

Jon Skeet
@Jon:whats the advantage using UTC time over local time?
Prashant
Well it depends on what the database is in, and what conversions are going on. The exact situation will determine which is the correct form to use.
Jon Skeet