IntelliSense Fosters Inattention

I count on IntelliSense a lot when programming - especially in C# which (arrggh!) is case-sensitive. However, I just had a case where IntelliSense led me to make a mistake that cost me half an hour.

I was happily working in LINQ and had the following code:

protected void LinqDataSource1_Deleting
    (object sender, LinqDataSourceDeleteEventArgs e)
{
    NWDataClassesDataContext dc = new NWDataClassesDataContext();
    Product prod;
    prod = (Product)e.OriginalObject;
    var q = from o in dc.Order_Details
            where o.ProductID == prod.ProductID
            select o;
    foreach (Order_Detail od in q)
    {
        dc.Order_Details.DeleteAllOnSubmit(od); // fails!
    }
    dc.SubmitChanges();
}

There was an error in the foreach loop:

The type arguments for method 'System.Data.Linq.Table<Order_Detail>.DeleteAllOnSubmit<TSubEntity>(System.Collections.Generic.IEnumerable<TSubEntity>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.  

After going in circles (and unsuccessfully searching the error message), I finally understood what the error was telling me: I had blithely accepted the DeleteAllOnSubmit() method which came first in the alphabetical list of members. The method that I really wanted was the next item in the IntelliSense listbox, DeleteOnSubmit() as shown here:

protected void LinqDataSource1_Deleting
    (object sender, LinqDataSourceDeleteEventArgs e)
{
    NWDataClassesDataContext dc = new NWDataClassesDataContext();
    Product prod;
    prod = (Product)e.OriginalObject;
    var q = from o in dc.Order_Details
            where o.ProductID == prod.ProductID
            select o;
    foreach (Order_Detail od in q)
    {
        dc.Order_Details.DeleteOnSubmit(od);
    }
    dc.SubmitChanges();
}

I probably wouldn't have goofed up if forced to type the method name rather than hitting Tab to select something from IntelliSense.

Hmmm. Perhaps I shouldn't admit that I lose time on silly mistakes like this?  Oh well. My hope is that someone else will find this blog entry while searching the error message and profit from my inattention.

6 Comments

  • Why not skip the foreach loop altogether and just do a:

    dc.Order_Details.DeleteAllOnSubmit(q);

    It's less code and IntelliSense would have been(though accidentally) right in the first place.

  • How in the world did this take 30 minutes to track down. :P

  • What you fail to mention is the amount of time intellisense saves you when it's right and/ or when you're paying attention. Over the course of a project, I guarantee it works out to more than a half hour of reduced code, especially if (like me) you've added in code snippets and the like.

  • @Martin: Good eye! If I hadn't been porting code, that would be perfect.

    @Daniel: It's not funny, is it? Seriously, I kept focusing on the code that precedes the problem line. I learned that debugging a LINQ query is not a pleasant experience... it's one big blob at the breakpoint!

    @Paul: I agree 100%. As I say, IntelliSense saves my butt a million times over, especially by providing the proper case and syntax in C#. It's just that - like copy and paste - the tool's effectiveness decreases when the problem occurs between the keyboard and the chair.

    Ken

  • I'm very happy you blogged this post Ken. I am trying to Learn ASP.NET MVC and LINQ from the new NerdDinner book and got hung up here. Your post allowed me to figure out the problem in my code and move on with my efforts. Thanks again!

  • @Ken: Thanks! That saved me a few minutes to find out what's the problem.

    @Daniel: You are right. I just realized sometimes we just turn to google every time we have an error message instead of thinking.
    On the other hand that is not exactly the error message that I would expect for an error like this.

Comments have been disabled for this content.