July 2008 - Posts
VB.Net to C# Converter is one of the few conversion tools that knows about LINQ and lambdas and the other 3.5 stuff. I turned to a trial version of the product after going in circles on this VB code conversion:
Dim q = From c In dc.Categories, _
p In dc.Products _
Where c.CategoryID = p.CategoryID _
Group p By c.CategoryName Into Group _
Select New With _
{.cgname = CategoryName, _
.prdcts = Group}
While other tools choked, VBConversions churned out this:
var q = (from c in dc.Categories
from p in dc.Products
where c.CategoryID == p.CategoryID
group p by c.CategoryName into g
let CategoryName = g.Key
let Group = g.ToArray()
select new { CategoryName, Group }).Select(s =>
new { cgname = s.CategoryName, prdcts = s.Group });
I'm not sure I would have figured that out on my own.
Actually, I fixed a small bug in the conversion. The original result had the assignment operator (=) instead of the comparison operator (==) in the where clause. When I reported this issue to the author, I got a prompt, polite, and appreciative response with the assurance of a fix in the next release.
One thing that I'd like to see is a snippet converter in the tool. Right now, you need to point the converter to a VS project file - something I don't normally use in file-based, ASP.NET 3.5 Web development.
For more info on VB.Net to C# Converter, see http://vbconversions.net/ .
BTW, I had to smile at C#'s use of "let" in LINQ syntax. Don't I remember Let from the earliest BASIC days?
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.
Alas, C#/VB language bigotry rears its ugly head again. My book, ASP.NET 3.5 For Dummies got its first negative review from a purchaser on Amazon.com solely because I used VB instead of C# in the examples. As I stated in the book's introduction, I chose VB because the book targets beginners who generally find VB easier.
Actually, C# fans are using the book quite successfully as evidenced by the way they've exchanged conversion tips on my support site. (I've posted C# versions of the source code for three chapters - with more to come.)
Interesting to note that Anand Narayanaswamy of ASPAlliance has posted a glowing review. He's a professional developer and reviewer who focuses on the book's content and is obviously not perturbed by my choice of language.
Okay, let's settle this language war the easy way - we'll all get right down to the metal by adopting to machine language!
Ken
Here I am debugging JavaScript in an ASP.NET 3.5 site using Visual Studio 2008. In the Command Window, I type:
? grid
to see the properties of a JavaScript object. So far so good until the Command window reports:
< More... (The first 100 of 429 items were displayed.) >
Back in the DOS days, I would have hit the spacebar to see the next 100 and so on. How do I do it in this window? Well, I haven't found a way in the Command window (which, to me is a bug/annoyance).
The workaround is to open a Watch window while at a breakpoint, and type the name of the object. That lists all of the members. That's much better, but it would be nice to click the Name column and do an alphabetical sort.
Hey folks, Microsoft has licensed four chapters of my book, ASP.NET 3.5 For Dummies! You can browse the chapters at http://msdn.microsoft.com/en-us/beginner/cc409659.aspx in the Microsoft Visual Studio Express Editions Beginner Developer Learning Centre.
Yup, MS has some of the best chapters and makes them available to you for free. You need the XPS viewer to read them.
- Chapter 5: Handling User Input and Events
In this Chapter we cover gathering data and pushing buttons, using drop-down lists and list boxes, presenting multiple choices, and sending data with forms.
- Chapter 7: LINQ as a Data Language
In this Chapter we look at using From, Where, and Select clauses, filtering, grouping, and narrowing scope, aggregating, creating and querying XML with LINQ, and using object initializers.
- Chapter 8: Using LINQ to SQL and the LinqDataSource
Diving deeper into LINQ, we'll cover using the object relational designer, filtering data in LinqDataSource, understanding LINQ to SQL syntax, grouping and displaying hierarchical data, updating and inserting with DataContext, and creating a user interface with the ListView control.
- Chapter 15: Enhancing Pages with the AJAX Control Toolkit
In this Chapter we cover completing data as users type, using a lookup Web service, masking and watermarking text boxes, creating a pop-up calendar, and keeping content on top.
Note that there's an error on the page: You need Visual Web Developer 2008 for LINQ, not 2005.
There's tons of free information for Web development beginners at MSDN. It's a great resource that has gone unnoticed (by me at least) for too long!
More Posts